I'm a little confused about how to load the NetSui...
# suitescript
s
I'm a little confused about how to load the NetSuite module into console, any docs that would explain, or someone could give me some tips
e

https://www.youtube.com/watch?v=ZAN8clhKxIw

Be aware not all modules/functions are available client-side
m
Here's some code I use to hoist modules in (both native and custom):
Copy code
// This is a helper function that I use while I am testing.  It is not in the helper module becuase it cannot be added to window from there
// The main purpose of it is to open console and quickly require a module and give it an obvious name.  Do not use it in actual scripts.
// examples:
//    debug_require('N/currentRecord')                => use window.currentRecord
//    debug_require('N/email', 'customName')          => use window.customName
//    debug_require('/Suitescripts/.../myScript.js')  => use window.myScript
//    debug_require('script_config_name')             => use a script defined in script_config_json
function debug_require(module, in_name)
{
    var script_config_json = {
        "paths": {
            "custom_module_1": "/path/to/module/1.js",
            "custom_module_2": "/path/to/module/2.js",
        }
    }

    var module_to_require = module;
    if(script_config_json.paths[module])
    {
        module_to_require = script_config_json.paths[module];
    }

    // require the module
    require([module_to_require], function(loaded)
    {
        // see if we provide a name
        var name = in_name;

        // if we don't provide a name, use the module as a name
        if(!name)
        {
            name = module
        }

        // only use a name after the last '/' symbol (works for 'N/record' as well as '/Suitescript/Libraries/...')
        name = name.slice(name.lastIndexOf(`/`)+1);
        
        // only use a name before the first '.' (works for 'stuff.js')
        name = name.replace(/\..*/g,'');

        // if there is a window
        if(window)
        {
            // if the original module requested was current record, call get, and then assign that to the window object
            if(module == 'N/currentRecord')
            {
                window[name] = loaded.get();
                console.group(`window.${name} loaded`);
                console.warn(`Do not use in actual scripts - only for debugging in console.`);
                console.warn(`In real usage of require([${in_name}]), you will need to call currentRecord.get() after require()ing.`)
                console.groupEnd(`window.${name}`);
            }
            // otherwise just assign it to the window
            else
            {
                window[name] = loaded;
                console.group(`window.${name} loaded`)
                console.warn(`Do not use in actual scripts - only for debugging in console.`);
                console.groupEnd(`window.${name}`);
            }
        }
    })
}
Three things to note though. (1) The scripts are not loaded immediately - you have to wait a little bit until you see the message saying they are loaded. There is no callback you can hook into because this is meant for debugging in console, not for actual scripts. (2) You will need to define a name and path in the script_config_json object at the top of the function for any custom modules you want to try loading in. (3) When loading
N/currrentRecord
, this call .get() and returns the currentRecord object automatically for you. This is because when you grab
N/currentRecord
you pretty much always want to call
.get()
and then do other things from there. And it's a pain to always have to call
.get()
.
And as eric said, note that some modules are server side only 🙂 - and also that some client side modules might behave a little differently server side.
Also as another note, though unrelated to your question, still good to know, there is one Suitescript 1 functionality that cannot be duplicated in Suitescript 2 - you can't edit line items that are not currently selected ( eg.
nlapiSetLineItemValue()
). If you think you might need to do this in client scripts, you might need to call Suitescript 1 methods from your Suitescript 2 scripts. Kinda stinks 😕.