N/runtime Module Script Samples
The following script samples demonstrate how to use the features of the N/runtime module:
Return User and Session Information
The following sample shows how to use a Suitelet to write user and session information for the currently executing script to the response.
This script sample uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
// This script writes user and session information for the currently executing script to the response.
define(['N/runtime'], function(runtime) {
function onRequest(context) {
var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
var userRole = runtime.getCurrentUser().role;
var currentSession = runtime.getCurrentSession();
// Set the current sessions's scope
currentSession.set({
name: 'scope',
value: 'global'
});
var sessionScope = runtime.getCurrentSession().get({
name: 'scope'
});
log.debug('Remaining Usage:', remainingUsage);
log.debug('Role:', userRole);
log.debug('Session Scope:', sessionScope);
context.response.write('Executing under role: ' + userRole
+ '. Session scope: ' + sessionScope + '.');
}
return {
onRequest: onRequest
};
});
Create Multiple Sales Records Using a Scheduled Script
The following sample shows how to use a scheduled script to create multiple sales records and log the record creation progress.
This script sample uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
// This script creates multiple sales records and logs the record creation progress.
define(['N/runtime', 'N/record'], function(runtime, record) {
return {
execute: function(context) {
var script = runtime.getCurrentScript();
for (x = 0; x < 500; x++) {
var rec = record.create({
type: record.Type.SALES_ORDER
});
script.percentComplete = (x * 100)/500;
log.debug({
title: 'New Sales Orders',
details: 'Record creation progress: ' + script.percentComplete + '%'
});
}
}
};
});