Write User and Session Information to the Response

A Suitelet to write user and session information for the currently executing script to the response:

Solution:

/**

 * @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

  };

});

Leave a comment

Your email address will not be published. Required fields are marked *