Javier Arroyo
Mega Guru

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); 

 

Comments
britwill
ServiceNow Employee
ServiceNow Employee
Jibarican-
 
As a fellow ServiceNow evangelist, thank you for taking the time to post your thoughts on coding best practices.
 
However, I sense your efforts are not being well-received as they should be.
 
To help promote your work, can you post real-world examples of ServiceNow solutions you've authored that solved a particular issue?
 
Regards,
britwill
 
Javier Arroyo
Mega Guru

Hi britwill,

Thank you so much for your thoughts and advice. As I begin to understand how the community works, I'll be adding more real-world posts. However, because of principle, I won't share company specific code. In turn I will attempt to talk about where I have used my methods, along with some POCs to go with it.

In general, I have used implicit usage of Current in Workflows, Business Rules, Notification Email Scripts to communicate with Script Includes.

In workflow activities to pass values from Current to task in a Script Include.

In business rules to bootstrap BRs to singletons that can be used by subsequent running BRs

In Notification Email Scripts to return notification content.

All of this is normally done within Single Scoped Applications. When dealing with cross scope scripting, I tend to use explicit calls, 1. because cross scripting is yet fully "JavaScriptified" (The rhino engine or JavaScript are not behaving as Rhino and ECMAScript specifications). 2. It informs me that I'm going either cross scope or staying within a single scope. I tend to think of it as part of the reability of my code

Hopefully, I have enough free time to provide useful real world examples.

Cheers and Thank You,
Jibarican 

jayesh_hoondlan
Tera Contributor

Great Article

Version history
Last update:
‎02-27-2018 12:15 PM
Updated by: