logo

NJP

Allow Execution Context to Pick up "current" record without explicitly passing it as a Class, or Func parameter

Import · Feb 27, 2018 · article

Having been indirectly convinced to post my approach to coding in ServiceNow by someone who knows far more than I do, I've decided to make this an initial of what I hope to turn into a series of posts revealing my coding practices. The idea is rooted around scale-able, less brittle, and easily maintainable code base. More importantly, have others see aspects of my code that I am not seeing.

The first approach is using an IIFE to automatically access objects within an execution context. Meaning that if A spawns B, then B has access to A, then C to B and A, etc.

Script Include as Module Pattern to separate concerns; each concern separated as IIFE that itself is a Revealing Module

var MyModule = {

namespaceone : ( function(current, previous) {

//code logic here such as.

function doSomePreSaveDataManipulation(){

current.short_description = "my short description";

}

var publicAPI = {

somePreSaveDataManipulation : doSomePreSaveDataManipulation();

}

return publicAPI;

})(current, previous);

};

The blow snippet can be a Business Rule, a Workflow Activity, etc. Currently, I'll go with a before business rule.

(function executeRule(current, previous){

/*access name space by reference. because JS behavior, objects found in the functions execution context will be automatically available to "children".*/ var manipulate = MyModule.namespaceone;

manipulate.somePreSaveDataManipulation();

})(current, previous);

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/allow-execution-context-to-pick-up-quot-current-quot-record/ta-p/2329737