Service Portal - \$sp.getRecordElements(.......) examples?

Constantine Kr1
Giga Guru

Hi everyone,

I am trying to use the $sp.getRecordElements(Object data, GlideRecord from, String names) method, but I am having very little success. I originally saw this used in another widget (the ServicePortal slushbucket widget); however, when I try to modify the code it fails.

To complicate the issue, the documentation on the developer.service-now.com conflicts with the ServicePortal Slush Bucket widgets usage of $sp.getRecordElements() . Example, service portal example has a return value from the method where the documentation states is a void.

Slush Bucket Widget - Server Script code :

(function () {

  // grab names of available dependencies

  data.all = [];

  var gr = new GlideRecord('sp_dependency');

  gr.orderBy('name');

  gr.query();

  while (gr.next()) {

  var t = $sp.getRecordElements(gr, 'name,sys_id');

  data.all.push(t);

  }

})();

Developer.service-now.com Documentation :

getRecordElements(Object data, GlideRecord from, String names)

For the specified fields, copies the element's name, display value, and value into the data parameter.
Parameter(s):

NameTypeDescription
dataObjectThe element's name, display value, and value for the specified fields are copied to this object.
fromGlideRecordThe GlideRecord to process.
namesStringA comma-separated list of field names.
Return:
TypeDescription
voidMethod does not return a value

3 REPLIES 3

diegochavez
Tera Contributor

did you modify the html template?   it was referencing "item.name.display_value" which was the field name being passed from the serverscript, so if that changes, you need to change the html as well.   before i did that,   I was just getting blank rows.   not sure if thats what you are running into as well but hope it helps.




https://community.servicenow.com/thread/280472


Thanks for the tip!  I missed the server switch name.

Arun Chauhan
Giga Contributor

Hi  Constantinekrick,

 

I tried the same in the scoped application and at first it was not working. Then I did some changes in the script code as below and it started working:

 

var t = {};
$sp.getRecordElements(t, gr, 'name,sys_id');
data.all.push(t);

 

As the documentation states, getRecordElements returns void, and it accepts three arguments. I created a variable and provided as the first argument and it is working now.