Skip to content

Instantly share code, notes, and snippets.

@michoelchaikin
Created July 24, 2020 02:44
Show Gist options
  • Save michoelchaikin/404b391a087126690105937bf6e65786 to your computer and use it in GitHub Desktop.
Save michoelchaikin/404b391a087126690105937bf6e65786 to your computer and use it in GitHub Desktop.
/**
* Usually if you want to access the SuiteScript 2 API via the browser console you need to open specific pages
* that load the required libraries (such as a record in edit mode). This script will load the libraries on any
* NetSuite page
*/
(async () => {
/* Add a script element to the DOM and wait until it's loading by checking that
a known variable it creates exists */
async function addScriptToDOM(src, scopeToCheck, variableNameToCheck) {
const scriptElement = document.createElement("script");
scriptElement.type = 'text/javascript';
scriptElement.src = src;
document.body.appendChild(scriptElement);
while(! scopeToCheck.hasOwnProperty(variableNameToCheck)) {
await new Promise(resolve => setTimeout(resolve, 50));
}
};
if(! window.require) {
await addScriptToDOM('/javascript/suitescript/lib/NsRequire.js', window, 'require');
await addScriptToDOM('/javascript/suitescript/bootstrap_shared.js', window, 'nlapi');
await addScriptToDOM('/javascript/suitescript/2.0/client/bootstrap.js', require, 'SSModuleLoader');
}
require(['N/runtime'], (runtime) => {
console.log(`Hello ${runtime.getCurrentUser().name}`);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment