DEV Community

Erick
Erick

Posted on

4 4

Netsuite SuiteScript 2.0 Client Side onLoad Hackery

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>";
            }
        }
    });
Enter fullscreen mode Exit fullscreen mode

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)
        }
    }

);
Enter fullscreen mode Exit fullscreen mode

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

Enjoy!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay