DEV Community

loading...

Netsuite SuiteScript 2.0 Client Side onLoad Hackery

Erick
I’m a self taught hack. I’ve been known to use a goto statement. I don’t take myself too seriously and I don’t expect you to take me too seriously. If I’m wrong or suck, I have thick skin.
・1 min read

banner

When my company moved from Suitescript 1.0 to 2.0 (because, well, why not?) one thing we needed was to listen to the browser's message listener from another browser tab when some event occurred.

In order to do that, I needed script to listen upon load. Netsuite doesn't provide a feature to let us load a client side script on page load very easily. We had to create a UserEvent script that would call a function on load that would then assign an INLINEHTML custom field type to the form with the required function.

Check it out

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */

define(['N/runtime'],
    function (runtime) {

        return {
            beforeLoad: beforeLoad
        };

        function beforeLoad(context) {

            if (runtime.executionContext.toUpperCase() == 'USERINTERFACE') {
                var inline = context.form.addField({
                    id: 'custpage_attachmessage',
                    label: 'not shown',
                    type: 'INLINEHTML',
                });
                inline.defaultValue = "<script>jQuery(function($){ require(['/SuiteScripts/clientSuiteScript.js'], function(mod){ mod.pageInit();});});</script>";
            }
        }
    });

Then the client suitescript will roughly look like this:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */

define(['N/currentRecord'],

    function (currentRecord) {

        function pageInit() {
            window.addEventListener("message", browser_ReceiveMessage, false);
            console.log('Added Listener...');

        }

        return {
            pageInit: pageInit
        }

        function browser_ReceiveMessage(event) {

            var curRec = currentRecord.get();
            console.log('Current Record ID', curRec.id)
        }
    }

);

Then on load, the console will output the currentRecord's id.

Enjoy!

Discussion (0)