Record Producer script to pass value of list collector to Configuration Item and Affected CI related list

JC S_
Mega Guru

We are trying to create a record producer wherein 1 variable in the form is a list collector. Now when the user submits the form it will should be able to parse the values entered on the variable (list collector) and do the following:

1) Set the first item entered (a configuration item in our case) as the value of the Configuration Item field in the change request record

2) Set the succeeding items as values on Affected CI related list

We figured this should be done via a script in the record producer, any ideas on how to do this?

Here's one script we found here on another community discussion How to map the list collector from the record producer into the change form(we need to split the lis... , we need a way for the values of sec_ci to be passed to Affected CI related list of change_request table:

var CIList = current.variables.<listCollectorName>;

var array = CIList.split(",");

var sec_ci = '';

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

if(i==0){

<Configuration Item field> = array[i]; // initialize Configuration Item field

}

else {

if (sec_ci != ''){sec_ci += ','+array[i];}

else {sec_ci = array[i];}

}

}

<secondary CI field> = sec_ci; // initialize secondary CI field

If only ServiceNow allows dot walking to the related lists when selecting a variable to map on Record Producers, then this would not need custom script development.

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Jc,



So you can do this in onSubmit script.



1) get the value of the field using g_form.getValue('<listCollectorVariable>')


2) do the necessary scripting and populate in target table.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The scripting part is where we need help on how to specifically achieve this.


I am not sure where are you running your code, but we need to take the remaining sys_id of CIs and then have to create a record in task_ci table. Please check if this helps.



var CIList = current.variables.<listCollectorName>;


var array = CIList.split(",");


var sec_ci = '';


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


if(i==0){


<Configuration Item field> = array[i]; // initialize Configuration Item field


}


else {


var gr = new GlideRecord('task_ci');


gr.initialize();


gr.ci_item = array[i];


gr.task = current.sys_id; //PASS THE SYS_ID OF CHANGE REQUEST


gr.insert();


}


}


Hi Shishir,



Just tried the code you modified but it inserted the 1st CI twice on the Affected CI related list instead. None of the 2nd, 3rd and so on CIs were inserted.


I am running this code on the script of the record producer.