Need to create record in "task_cmdb_ci_service" table with the listcollector values

Vinay49
Tera Expert

Hello Community Leaders,

 

We have one List collector variable which is refering to cmdb_ci table to select multiple ci's in one of catalog item. when user select CI's and submit, The change record should be created  & also "task_cmdb_ci_service" should be created with the values of CI's which i selected when request was submitted & with the change number.

 

I have created workflow for the same. 

find_real_file.png

to create change record i am usign create task activity & to create record in "task_cmdb_ci_service" table i am using run script activity with below script.

 

var gr = new GlideRecord('task_cmdb_ci_service');
gr.initialize();
gr.cmdb_ci_service = current.variables.ci;
gr.task = workflow.scratchpad.number;// this number is from "create task activity"
gr.insert();

 

But it is not working as expected. Please help me here.

 

Thanks in advance

6 REPLIES 6

Sai Kumar B
Mega Sage
Mega Sage

Hi,

Try the below script and store task sys_id in scratchpad instead number in "create task activity"

var listValues = current.variables.ci.toString();
var split = listValues.split(',');
for(var i in split){ //Iterates over every CI and creates Individual record for every CI
var gr = new GlideRecord('task_cmdb_ci_service');
gr.initialize();
gr.cmdb_ci_service = split[i]; //Stores individual CI as service
gr.task = workflow.scratchpad.sys_id;// this sys_id is from "create task activity"
gr.insert();
} //End of the for loop

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

var arr = current.variables.ci.toString().split(',');

for(var i=0;i<arr.length;i++){
	var gr = new GlideRecord('task_cmdb_ci_service');
	gr.initialize();
	gr.cmdb_ci_service = arr[i];
	gr.setDisplayValue('task',workflow.scratchpad.number);// this number is from "create task activity"
	gr.insert();

}

OR

Store the task sys_id in workflow scratchpad instead of number and use this script

var arr = current.variables.ci.toString().split(',');

for(var i=0;i<arr.length;i++){
    var gr = new GlideRecord('task_cmdb_ci_service');
    gr.initialize();
    gr.cmdb_ci_service = arr[i];
    gr.setValue('task',workflow.scratchpad.sysId);// this number is from "create task activity"
    gr.insert();

}

Regards
Ankur

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

HI Ankur,

 

Thanks for your quick response,

 

 I have tried with the scripts which you shared. but some how it was not working. it is not creating records in the "task_cmdb_ci_service" list.

Any suggestions here.

 

Thanks in advance

Please share your latest script

Did you use sys_id to set?

Regards
Ankur

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