netsuite User Event: Before Load event Restrict execution based on the context that triggered the User Event

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

In SuiteScript 1.0, we retrieve the current execution context using nlapiGetContext().getExecutionContext(), then we compare the result to the appropriate raw Strings.

// 1.0 in Revealing Module pattern
var myNamespace = myNamespace || {};
myNamespace.example = (function () {
    var exports = {};
    
    function beforeLoad(type, form, request) {
        showBonusEligibility(form);
    }
    
    function showBonusEligibility(form) {
        // Doesn't make sense to modify UI form when the request
        // did not come from the UI
        var currentContext = nlapiGetContext().getExecutionContext();
        if (!wasTriggeredFromUi(currentContext)) {
            return;
        }
        
        // Continue with form modification...
    }
    
    function wasTriggeredFromUi(context) {
        // Current context must be compared to raw Strings
        return (context === "userinterface");
    }
    
    function isEligibleForBonus() {
        return true;
    }
    
    exports.beforeLoad = beforeLoad;
    return exports;
})();

In SuiteScript 2.0, we get the current execution context by importing the N/runtime module and inspecting its executionContext property. We can then compare its value to the values of the runtime.ContextType enumeration rather than raw Strings.

// 2.0
/**
 * @NScriptType UserEventScript
 * @NApiVersion 2.x
 */
define(["N/ui/serverWidget", "N/runtime"], function (ui, runtime) {
    var exports = {};
    
    function beforeLoad(scriptContext) {
        showBonusEligibility(scriptContext.form);
    }
    
    function showBonusEligibility(form) {
        // Doesn't make sense to modify the form if the 
        if (!wasTriggeredFromUi(runtime.executionContext)) {
            return;
        }
        
        // Continue with form modification...
    }

    function wasTriggeredFromUi(context) {
        // Context can be compared to enumeration from runtime module
        return (context === runtime.ContextType.USER_INTERFACE);
    }
    
    exports.beforeLoad = beforeLoad;
    return exports;
});


Got any netsuite Question?