Javier Arroyo
Mega Guru

There are times that a quick list of display values are needed. Maybe it's just to snoop around, or list RITMS under a request in an email. Whatever the case might be, there is an quick way to do so:

var gr = new GlideRecord('sc_task');
gr.setLimit(10);
gr.query();

var fields = 'sys_id,number,short_description,assignment_group'.split(',');

while( gr.next() ){
    gs.debug( fields.map( gr.getDisplayValue.bind(gr)));

}

Result:

35decdebdb0e58948870894d0b961937,SCTASK0010716,Some Name: General Request: aaa - REQ0010608,Apps_SNOW_AG_P72Support
565153851b859010b9f3fc49cc4bcbe8,SCTASK0010582,Fulfill the user's request,Apps_SNOW_AG_P72Support
59954b79dbca90548870894d0b96191d,SCTASK0010715,Another Name: General Request: Friday 2 - REQ0010607,Apps_SNOW_AG_Automated_Requests
5eb9d496dbfc04108870894d0b961942,SCTASK0010006,Release to PROD,
6354b4101b295c10b9f3fc49cc4bcb5e,SCTASK0010677,Some Name: General Request: Test - REQ0010573,Apps_SNOW_AG_P72Support
6452cb201b670490b9f3fc49cc4bcb31,SCTASK0010529,My Name: General Request: this is a short desc - REQ0010420,Apps_SNOW_AG_PH_Support
81abd0aedb15230087999007db96190a,SCTASK0010029,Assess or Scope Task,
88b9d056dbfc04108870894d0b9619d5,SCTASK0010005,Test requirements in TEST,
8af3975cdb5d98148870894d0b96193e,SCTASK0010636,Some Name: Hardware Request,Apps_SNOW_AG_TradeSupport-CT
8d895710db25230087999007db9619b4,SCTASK0010033,Assess or Scope Task,

 

It works because of the bind operation being called on getDisplayValue. Without bind the call wouldn't retrieve the display values. This is because whenever a method (object.functionName) is passed in to a function, it loses the object to which it was bound. Using bind on getDisplayValue says: hey, go on your own but, don't forget you belong to me so that when you do get executed. You are executed within me and no one else. Me in this says is whatever parameter is passed into bind. In this test case it was gr.  

While it has it downsides, the strategy can be useful from time to time.

Happy SNowing...

Comments
Fabian Kunzke
Mega Sage
Mega Sage

ServiceNows greatest coding evangelist is back! Noice.

Javier Arroyo
Mega Guru
You are too kind, Fabian. Thank you.
Version history
Last update:
‎08-28-2020 11:57 PM
Updated by: