Issue with Extracting Sys IDs from List Collector in Workflow Script

RanjanRP
Tera Contributor

Hi all,

 

I'm trying to extract Sys IDs from a List Collector variable in a ServiceNow workflow script and then use those sys id to glide into the record to get particular values. The same code works fine in Background Scripts, but in the workflow it does not work

 

var sysIdArray = sysregion.split(','); code is what which does not work in workflow 
 
thanks
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

It's likely running into a problem trying to execute a string method (split) on a value that is not interpreted as a string, so try something more like this - in the context of whatever makes sense in the entire script - to force the list collector variable value to a string before splitting it:

var sysIdArray = current.variables.sysregion.toString().split(',');

 

View solution in original post

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

Hi @RanjanRP,

Please share the code screen shot which you added in workflow.

The code will work on background, if you explictly added sys_ids' with comma seperated values.

 

-Thanks,

AshishKM

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Brad Bowman
Kilo Patron
Kilo Patron

It's likely running into a problem trying to execute a string method (split) on a value that is not interpreted as a string, so try something more like this - in the context of whatever makes sense in the entire script - to force the list collector variable value to a string before splitting it:

var sysIdArray = current.variables.sysregion.toString().split(',');

 

RanjanRP
Tera Contributor

below is the code am using

 

var sysregion = current.variables.please_select_all_of_the_regions_that_apply; //list collector 
var resultsArray = [];
var gr = new GlideRecord('u_req_lookups');
var sysIds = sysregion.split(',');
for (var i = 0; i < sysIds.length; i++) {
var sys_id = sysIds[i];
 gr.initialize();
 gr.addQuery('sys_id', sys_id);
 gr.query();
while (gr.next()) {
 resultsArray.push(gr.u_dependantvalue1 + '');
 }

}
var regionsString = resultsArray.join(', ');
current.variables.selectedregions = regionsString;

In addition to forcing to string, only use the initialize() method when creating a new record, and you don't even need/want an array in this case - just use the comma-separated list of sys_ids that is the list collector value:

var sysregion = current.variables.please_select_all_of_the_regions_that_apply.toString(); //list collector 
var resultsArray = [];
var gr = new GlideRecord('u_req_lookups');
gr.addQuery('sys_id', 'IN', sysregion);
gr.query();
while (gr.next()) {
    resultsArray.push(gr.u_dependantvalue1.toString());
}

var regionsString = resultsArray.join(', ');
current.variables.selectedregions = regionsString;