Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

dougconnell
Kilo Guru

for Service Catalogue items, it is sometimes useful to be able to get additional information from variables - such as other fields from tables used by Reference Fields.  How do you do this?  I spent hours, possibly days trying to understand: GlideRecord, GlideElement, GlideElementVariable and their associated APIs.  GlideElementVariable is an especially difficult class.  There is no API documented.  I am guessing slightly (perhaps someone with more knowledge can correct me) that this class is a Java class so the API is deliberately not published and not made available in Javascript.  "Hide the Implementation" is an important concept - so I understand.  I did discover some useful Javascript functions on the communities but they had been removed by SN and were no longer supported.  One key method is the getQuestion() method - which I only recently discovered.  This is useful for finding the name of the reference table as shown in the Test Background Script below.  This script also helps to explain a little the hierarchy and APIs in this area.

Please remember to change the RITM number below.

function getRequestedItem(reqItem) {
   var gr = new GlideRecord('sc_req_item');
   gr.addQuery('number',reqItem);
   gr.query();
   while ( gr.next() ) {
      var vars = gr.variables.getElements(true);
      for (var i=0; i<vars.length; i++) {
        var variable = vars[i];
        if ( variable.getValue() ) {
		  debugText += "\n";
          debugText += ", class=" + variable.constructor.name;
		  var question = variable.getQuestion();
		  debugText += ", name=" + question.name;
		  debugText += ", type=" + question.type;
		  debugText += ", reference=" + question.reference;
		  debugText += ", value=" + variable.getValue();
		  debugText += ", displayValue=" + variable.getDisplayValue();
		  
        }
      }
   }
}

var debugText = "ADC:";
var res = getRequestedItem('RITM1026595');
gs.log(debugText);

 

I go this output:

*** Script: ADC:
, class=GlideElementVariable, name=requested_for, type=8, reference=sys_user, value=9813c891dbb443dfb000f6e9bf961962, displayValue=John Smith
, class=GlideElementVariable, name=requested_for_country, type=18, reference=undefined, value=NZ, displayValue=Country | ABC
, class=GlideElementVariable, name=requester_for_user_id, type=6, reference=undefined, value=conneld3, displayValue=smith3
, class=GlideElementVariable, name=requester_for_email, type=6, reference=undefined, value=John Smith@abc.com, displayValue=ohn Smith@abc.com
, class=GlideElementVariable, name=requester_for_emp_num, type=6, reference=undefined, value=818436, displayValue=818123
, class=GlideElementVariable, name=shared_folder_location, type=5, reference=undefined, value=global, displayValue=Global
, class=GlideElementVariable, name=request_type, type=5, reference=undefined, value=create_new, displayValue=Create New Shared Folder
, class=GlideElementVariable, name=shared_folder_name, type=6, reference=undefined, value=Doug, displayValue=John
, class=GlideElementVariable, name=declar_sf_creation, type=5, reference=undefined, value=yes, displayValue=Yes
, class=GlideElementVariable, name=shared_folder_path, type=6, reference=undefined, value=\\XXXXXXXXXXXXX.asia.corp.anz.com\doug, displayValue=\\XXXXXXXXXXXX.asia.corp.anz.com\doug
, class=GlideElementVariable, name=restricted_members, type=21, reference=undefined, value=d01de460db620df03fbd788bbf9619f7,2e1e55c5dbdf6b84f1a913153a9619ef, displayValue=John Smith, John Smith2
, class=GlideElementVariable, name=folder_classification, type=5, reference=undefined, value=public, displayValue=Public
, class=GlideElementVariable, name=folder_owner_name, type=8, reference=sys_user, value=d5fba8adfb2207003fbd788bbf9619ab, displayValue=John Smith
, class=GlideElementVariable, name=delegate_name, type=8, reference=sys_user, value=d5fba8a8db220df03fbddfbbf9619ab, displayValue=John Smith2
, class=GlideElementVariable, name=entity, type=6, reference=undefined, value=Hello, displayValue=Hello
, class=GlideElementVariable, name=business_unit, type=6, reference=undefined, value=Hello, displayValue=Hello
, class=GlideElementVariable, name=access_type, type=5, reference=undefined, value=grant_access, displayValue=Grant Access 
, class=GlideElementVariable, name=grant_access_control_type, type=5, reference=undefined, value=modify, displayValue=Modify
Comments
Gary Kakazu1
Giga Guru

Thanks for posting this. It was very helpful and saved me a lot of time.

Version history
Last update:
‎03-18-2020 02:51 PM
Updated by: