The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
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:    ADVANCED

Assumes having taken the class SSNF and has good intermediate to advanced level of knowledge and/or familiarity with Scripting in ServiceNow.


So now that I have this nifty way of turning a GlideRecord into an object array, AND I have that in a Script Include function library, what's next?   Well, how about extending the GlideRecord object to include the new toObjectList function?

 

I have seen things like this done in ServiceNow to extend the JavaScript language, but they use a Global Business rule which loads with every table call (no matter what). This type of implementation adds to the overall activity load and does not put forth the extension into every corner of ServiceNow. I suggest a different approach, lets write a Script Include that we will call on demand that provides the requested functionality. No performance load and it goes away when our code has completed!

 

NOTE: There are two DIFFERENT versions of GlideRecord: Global and Scoped. The Scoped version is a sub-set of the Global functionality and is locked so that you cannot extend it. One of its unfortunate failings, in my book. This particular set of labs will NOT work in the Scoped version. Sorry.

 

New stuff introduced with this article:

 

GlideRecord.prototype.

GlideRecord.restorelocation()

gs.include(...)

JSUtil.describeObject(...)

 

Lab 1.1 - Create a Script Include to Extend GlideRecord

With this lab we will be creating a Script Include function library that we will call with a gs.include statement.

 

1. Navigate to System Definition -> Script Includes. This will display the Script Includes list view.

2. Click on the New button.   This will display the new Script Include form.

3. Fill in the form with the following:

  1. Name: GlideRecordExtensions (this will trigger the creation of a Script template; delete this before entering in the script below).
  2. Client Callable: False
  3. Active: True
  4. Script:

 

// These functions will be tacked onto the GlideRecord object when we new it.
// You can add functions and properties to existing objects in this manner
// The "this" variable is actually the GlideRecord object!
GlideRecord.prototype.toObjectList = function() {
	var objectList = [];

	// loop through all the records and create the object array
	while(this.next()) {
		objectList.push(this.toObject(this));
	}
	this.restoreLocation(); // set it back to the beginning so that we can use if for other things

	return objectList;
};

// Turn a single GlideRecord record into an object
GlideRecord.prototype.toObject = function(recordToPackage) {
	 var packageToSend = {};

	 for (var property in recordToPackage) {
		try {
			packageToSend[property] = recordToPackage[property].getDisplayValue();
		}
		catch(err){}
	 }

	 return packageToSend;
};

 

Lab 1.2 - Testing the New Extensions

1. Navigate to System Definition -> Fix Scripts.   This will display the Fix Scripts list view.

2. Click on the New button.   This will display a new Fix Scripts form.

3. Fill in the form with the following:

  1. Name: GlideRecordExtension Test
  2. Active: true
  3. Script:

 

// This brings in our new functionality (so to speak)
gs.include('GlideRecordExtensions');

var incidents = new GlideRecord('incident');
incidents.addActiveQuery();
incidents.setLimit(10);
incidents.query();

gs.info(incidents.getRowCount());

// Now invoke our new GlideRecord extension!
var stuffList = incidents.toObjectList();

// Check to see if our list is now populated
gs.info(stuffList.length);

// It is! Now pick one of the objects out of the array and see if it's properties are populated
gs.info(stuffList[5].number);

// They are! Now describe that entire object to see if it was a fluke
gs.info(JSUtil.describeObject(stuffList[5]));

 

Everything is filled in like it should be!

 

4.  Click the Submit button to save your work.   The Fix Scripts list view will be displayed.

5.  Click on the name of your new Fix Script.   This will display your Fix Script for editing.

6.  At the bottom of the form click on the Run Fix Script link in the Related Lists section.   This will display the run Run Fix Script form.

7.  Click the Ok button to continue. This will display the warning form.

8.  Click the Proceed button to continue.   You should have a results form that displays something like the following:

 

*** Script: 10
*** Script: 10
*** Script: INC0000017
*** Script: Log Object Object sys_meta: null = undefined promoted_by: string = parent: string = caused_by: string = watch_list: string = upon_reject: string = sys_updated_on: string = 2015-11-24 09:47:36 origin_table: string = approval_history: string = skills: string = proposed_by: string = number: string = INC0000017 u_requester: string = Joe Employee (employee) lessons_learned: string = u_rca: string = state: string = Awaiting Problem sys_created_by: string = don.goodliffe knowledge: string = false order: string = delivery_plan: string = cmdb_ci: string =

NOTE: I stopped the results there as reproducing the whole result would have been a bore at best!

 

Now that wasn't so bad. In the next article I will use this functionality along with an array method called .filter to show off yet another nifty bit of capability.

 

NOTE: I placed all of these new prototypes into a GlideRecordExtensions Script Include. Check it out!

 

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: 10-06-2015 01:28 PM

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

6 Comments