can we define a variable outside function in Suite...
# suitescript
b
can we define a variable outside function in SuiteScript 2.1?
Copy code
define(['N/currentRecord', 'N/record'],

    (currentRecord, record) => {

        const token = "API";
        const apikey = "KEY";


        const beforeSubmit = (scriptContext) => {}
return {beforeSubmit}
});
Somehow this worked before, but when I try to save it without any change, now it shows me an error that
Fail to evaluate script: All SuiteScript API Modules are unavailable while executing your define callback.
Could anyone help me please?
c
IMO You should be using secrets management for this and not global variables. Or at least a script parameter but that's not secure in plain text. If you have a global there needs to be a pretty good reason for doing it.
b
It's just variable name I just tried. not real token
d
is that actually the full script contents? if it is truly not using Suitescript Api module outside of entry point functions (beforeLoad, before/afterSubmit) then it shouldn't be getting that error - normal variables as your example showed should be fine aside from that, N/currentRecord module is not typically used in serverside/userEvent script situations
b
@dbarnett actually I loaded library file which is also a SuiteScript file
Copy code
define(['N/currentRecord', 'N/record', './config'],

    (currentRecord, record, config) => {

        const token = config.getOptions();
        const apikey = "KEY";


        const beforeSubmit = (scriptContext) => { // Use that token}
return {beforeSubmit}
});
So full code snippet is here.
d
ah ok yes, so may need to put the call to
config.getOptions()
inside entry point function (if your custom config module itself calls Suitescript Api modules within likely)
👆 2