8 SuiteScript 2.0 in the Console
Created: June 19, 2017
In this edition, we take a brief look at how to test out SuiteScript 2.0 code in our browser console.
We can use our browser's Developer Tools in order to quickly test and experiment with SuiteScript without creating an entirely new Script.
In SuiteScript 1.0, all of our functions and Objects were available globally, so you could just pop open the console and start using SuiteScript. The module structure of SuiteScript 2.0 adds just a step or two to this process.
In order to work with a 2.0 module in the console, we need to import it into the console context first. We do that using the require function:
Note this looks a little like a normal module definition, except we use require instead of define, and we don't need to define a callback function.
This statement simply makes the specified modules available to load in the console; they haven't actually been loaded yet. If you have not yet run this statement to make the module available, you will receive this error when you try to load it:
We can now use require again to actually load each module:
Once loaded, we can use these modules just like we would from within a script. For instance, I can use the currentRecord module to retrieve field values from the record:
Or I can use the message module to display a message to the user:
You will want to be aware of a couple limitations of SuiteScript in the console.
First, not all modules are available client-side. If you try to load a module that is not available, you will receive a "load timeout" error:
Second, you can only run SuiteScript in the console from a page that already supports SuiteScript. I typically go to the entry form for a new record, like a Sales Order or Customer. If you try to load a module on a page that does not support SuiteScript, you will receive the following error:
That's all there is to it. Hopefully this makes your SuiteScript 2.0 testing and experimentation just a bit faster and easier.