Business rule to add items to a list collector field?

Shao
ServiceNow Employee
ServiceNow Employee

Hi,

I have a list collector field on a custom table. I would like to add on records from a reference table to this list whenever a filter criteria is met. My current business rule will trigger and replace all content of this list field instead of adding the records to the list. Would appreciate any help on a business rule script to meet this objective. Thank you! 

 
1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, without specific details of what\where\how you are doing this, it is hard to provide exact details but your basic script might be something like this

var myReferenceArray = [];

//Query your reference data
var mySource = new GlideRecord('mySourceTable');
mySource.addQuery('field', referenceValue);

mySource.query();

//iterate through the results and push records into an array
while(mySource.next() {
myReferenceArray.push(mySource.sys_id.toString());
}

//Now convert the array to a string and populate to the list field.
var myTargetString = myReferenceArray.toString();

myTargetList = myTargetString;

View solution in original post

2 REPLIES 2

AnirudhKumar
Mega Sage
Mega Sage

Please share your existing script ... it will be easier to modify something rather than start from scratch

Tony Chatfield1
Kilo Patron

Hi, without specific details of what\where\how you are doing this, it is hard to provide exact details but your basic script might be something like this

var myReferenceArray = [];

//Query your reference data
var mySource = new GlideRecord('mySourceTable');
mySource.addQuery('field', referenceValue);

mySource.query();

//iterate through the results and push records into an array
while(mySource.next() {
myReferenceArray.push(mySource.sys_id.toString());
}

//Now convert the array to a string and populate to the list field.
var myTargetString = myReferenceArray.toString();

myTargetList = myTargetString;