sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    BEGINNER
Assumes good beginner level knowledge and/or familiarity of Scripting in ServiceNow.


Here is a nifty bit of functionality to add to your toolbox.  

 

JavaScript passes most variable parameters by value. What this means is that if you pass in a string, integer, float or anything it comes in as a copy. Most other objects are passed by reference; such as user defined, and GlideRecord objects.

 

Example of passing by value:

 

var numberOfPeople = 5;

gs.info('---> Number of people: {0}', numberOfPeople);   // will print 5.
changeNumberOfPeople(numberOfPeople);
gs.info('---> [After] Number of people: {0}', numberOfPeople);   // will print 5.

function changeNumberOfPeople(numberOfPeople) {
      numberOfPeople = 10;
      gs.info('---> [inside function] Number of people: {0}', numberOfPeople);   // will print 10.
}

 

Results:

*** Script: ---> Number of people: 5
*** Script: ---> [inside function] Number of people: 10
*** Script: ---> [After] Number of people: 5

As you can see, it is a copy of the value and since we are not returning the updated variable it is not reflected in the calling code-stream. The scope is either "in" or "outside of" the function and one does not see the other. So, if you change the inside value it does NOT affect the outside value.

 

Now let's take a look at an object passing by reference. A pointer to the original variable is passed; not a copy. Therefore, if you modify the "passed-in" object it modifies the original.

 

Example of passing by reference:

 

var person = {};
person.arms = 2;
person.legs = 2;
person.heads = 1;

gs.info('---> [outside] number of heads: {0}', person.heads);
changePerson(person);
gs.info('---> [after] number of heads: {0}', person.heads);

function changePerson(personToChange) {
      personToChange.heads = 2;
}

 

Results:

*** Script: ---> [outside] number of heads: 1
*** Script: ---> [after] number of heads: 2

This works the same way with GlideRecords:

 

var incidentRecords = new GlideRecord('incident');
incidentRecords.setWorkflow(false);
if (incidentRecords.get('number','INC0010023')) {
      gs.info('---> [before] impact: {0}', incidentRecords.impact.getDisplayValue());
      changeIncident(incidentRecords);
      gs.info('---> [after] impact: {0}', incidentRecords.impact.getDisplayValue());

      incidentRecords.update();
}

function changeIncident(incident) {
      incident.impact = 1;
}

 

NOTE: I added the setWorkflow method in the GlideRecord to keep the business rules from firing. You normally don't want to do this, but my PDI is old and throws weird errors sometimes, and got tired of looking at them! 😊

 

Results:

*** Script: ---> [before] impact: 3 - Low
*** Script: ---> [after] impact: 1 - High

In conclusion: I use objects a LOT for the pass-by-reference reason. I can return multiple variables just by returning the object vs. any other method. This is a huge deal for working with a lot of variables. Objects allow you to seriously organize and control a lot of variables.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_1-1696102033098.png


Originally published on: <u+200e>08-18-2015 10:59 AM

I updated the code and brought the article into alignment with my new formatting standard.

3 Comments