Script to list all Catalog Items within the parent Request.

Wesley Breshear
Tera Expert

Hello,

I am trying copy all the Request Items number and short description name, to the description field and comments field of the Request (parent) when the Request is created.   This will hopefully help users understand what items were ordered in their Request.

So I am adding an 'Activity Properties: Run Script' to the OOB Workflow 'Service Catalog Request' and have tried to find scripts in the community that might work; I am weak at JavaScript.   I am not positive but I am thinking that my gr.addQuery('request',current.sys_id); is getting the Workflow sys_id and not the Request's sys_id, but I am not positive.   Do you know how I can get the Request's sys_id or what may be wrong with my script?

function getItems() {

var gr = new GlideRecord('sc_req_item');

gr.addQuery('request',current.sys_id);

gr.query();

while(gr.next())

gr += '\n' + gr.number() + ": " + gr.getDisplayValue();

//gr += '\n' + gr.number() + ": " + gr.getDisplayValue() + "- " + gr.short_description.getValue();

}

current.description = gr

current.comments = gr

Thank you,

Wesley

1 ACCEPTED SOLUTION

Use this code



var arr=[];


var gr = new GlideRecord('sc_req_item');


gr.addQuery('request',current.sys_id);


gr.query();


while(gr.next()){


arr.push(gr.number+': '+gr.short_description);


}


current.description='REQUESTED Items:'+arr.join("\n");


current.comments='REQUESTED Items:'+arr.join("\n");


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Wesley,



Use the below script.


var gr = new GlideRecord('sc_req_item');


gr.addQuery('request',current.sys_id);


gr.query();




while(gr.next())


{


current.description = gr.number;


current.comments = gr.short_description;


}







Hi Pradeep,



Awesome!   It is working for the 'Description' field, but having problems in writing to the Additional Comments or Work Notes field.   Neither work, am I missing something?



var gr = new GlideRecord('sc_req_item');


gr.addQuery('request',current.sys_id);


gr.query();



current.description = 'REQUESTED ITEMS:';



while(gr.next())


var cd = current.description + '\n';


current.description = cd + gr.number + ":   " + gr.short_description;


current.comments = gr.short_description;


current.work_notes = gr.short_description;



Thank you,


-Wesley


Use this code



var arr=[];


var gr = new GlideRecord('sc_req_item');


gr.addQuery('request',current.sys_id);


gr.query();


while(gr.next()){


arr.push(gr.number+': '+gr.short_description);


}


current.description='REQUESTED Items:'+arr.join("\n");


current.comments='REQUESTED Items:'+arr.join("\n");


Hi Abhinay,



Bingo, it works!   Thanks!   If I have it correct, to write to the Additional Comments or Work Notes fields my data needed to be put into an array?   Is that correct?



-Wesley