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.

Loop reference list object

nixu
Kilo Explorer

Hi All,

I have a table with one filed that is a list reference to another table, it would reference multiple objects.

I want to loop all object in this filed and do some work but I don't know how to do this

I tried two way below , but both were not work.

for(var item in current.items)

        gs.addInfoMessage(item.id);

current.items.split(',').forEach(function(item){  

        gs.addInfoMessage(item.id);

});

Is there something I missed?

Any help will be appreciated.

Thanks in advance.

3 REPLIES 3

Matthew Glenn
Kilo Sage

This is untested, but the following should work



Create an array from current.items


var arr = current.items.split(",");



Then loop through it using the following:


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


      //do your thing


}


Hi Matthew



I use


var arr = current.items.split(",");


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


      gs.addInfoMessage(arr[i].id);


}



but still get 'undefined' message


Since you're trying to pull a field from the records in the list, try this:



var arr = current.items.split(",");


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


      var gr = new GlideRecord('TABLE_NAME');   //insert the table that the list field references


      if(gr.get(arr[i])){


              gs.addInfoMessage(gr.id);


      }


}