getFields is not working in scoped application

kumarsatyam
Tera Expert

is there any substitute   of getFields in scoped application as it is giving error. Please specify the use . Can getFields can be used in Script Include (Scoped App) .

6 REPLIES 6

coryseering
ServiceNow Employee
ServiceNow Employee

GlideRecord also has a getElements method. This returns an array (if in scope) or an ArrayList (if in global) of all of the elements on the record. You can then loop through the array (in scope) or iterate through it (in global) to get things like the label, field name, and value.



var incident = new GlideRecord("incident");


incident.addActiveQuery();


incident.setLimit(1);


incident.query();


incident.next();




var elements = incident.getElements();


if (typeof elements.size != 'undefined') {


      //we are in global scope, so iterate


      for (var i=0; i<elements.size(); i++) {


              var element = elements.get(i);


              gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));


      }


} else {


      //we are in scope, so loop over the array


      for (var i=0; i<elements.length; i++) {


              var element = elements[i];


              gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));


      }


}






The above script should work in both global and scope.


ytrottier
Tera Contributor

That's a nice bit of code. Thanks for sharing that.

I find it particularly interesting that the returned object is different between global and scoped applications.

Anyone knows why it was done that way ?

I am just curious to know what would lead to that difference, if anyone knows.

 

Also, does anyone knows why getElements() and GlideScriptRecordUtil are not documented ?

Does it mean that we should not use those methods and classes, or is it simply that they are missing from the documentation ?

SN should certainly add that to their doc, given they already have getFields() and GlideRecordUtil.

 

Thanks.