The CreatorCon Call for Content is officially open! Get started here.

Add 2nd RITM to REQ via add cart api script

KristyS
Kilo Guru

Within a parent request and based on an if condition, i would like to create a second RITM using the Cart API script.   

I am able to create a separate request using the cart API however would like to create a second RITM instead to ensure both requests are linked together.  

 

find_real_file.png

1 ACCEPTED SOLUTION

KristyS
Kilo Guru

Thanks, to @randrews solution on Linked Post

 

I was able to use the following code to create a second RITM on the current REQ.  I'm still struggling with bypassing a switch statement on the 2nd RITM but i'm much closer.   

 

//create a new item in the request

var CN = 'a391b983db497200e5f83220ad961958'; //sid for New Computer Catalog item

var reqHelper = new GlideappCalculationHelper();


reqHelper.addItemToExistingRequest(current.request, CN, 1); // 1 is the qty


reqHelper.rebalanceRequest(current.request);

 

//find the item and update its variables


var grReqItem = new GlideRecord('sc_req_item');


grReqItem.addQuery('request', current.request);


grReqItem.addQuery('cat_item', CN);


grReqItem.addQuery('parent','');


grReqItem.query();


if(grReqItem.next()) {


grReqItem.variables.requester = current.variables.requester;

grReqItem.variables.requested_for = current.variables.requested_for;

grReqItem.variables.location = current.variables.requested_for.location;

grReqItem.variables.type_of_workstation = workflow.scratchpad.computertype;

gs.log("request item "+ grReqItem.variables.type_of_workstation);

grReqItem.variables.role = workflow.scratchpad.role;

grReqItem.variables.branch_suite_device = 'No';

grReqItem.variables.comments_box = workflow.scratchpad.comments;

grReqItem.parent = current.sys_id;


grReqItem.update();

}

 

 

If you have a switch statement at the beginning of the second workflow then you must add a 5 second timer to allow the switch statement to receive the value from the parent request.   

 

View solution in original post

7 REPLIES 7

KristyS
Kilo Guru

Thanks, to @randrews solution on Linked Post

 

I was able to use the following code to create a second RITM on the current REQ.  I'm still struggling with bypassing a switch statement on the 2nd RITM but i'm much closer.   

 

//create a new item in the request

var CN = 'a391b983db497200e5f83220ad961958'; //sid for New Computer Catalog item

var reqHelper = new GlideappCalculationHelper();


reqHelper.addItemToExistingRequest(current.request, CN, 1); // 1 is the qty


reqHelper.rebalanceRequest(current.request);

 

//find the item and update its variables


var grReqItem = new GlideRecord('sc_req_item');


grReqItem.addQuery('request', current.request);


grReqItem.addQuery('cat_item', CN);


grReqItem.addQuery('parent','');


grReqItem.query();


if(grReqItem.next()) {


grReqItem.variables.requester = current.variables.requester;

grReqItem.variables.requested_for = current.variables.requested_for;

grReqItem.variables.location = current.variables.requested_for.location;

grReqItem.variables.type_of_workstation = workflow.scratchpad.computertype;

gs.log("request item "+ grReqItem.variables.type_of_workstation);

grReqItem.variables.role = workflow.scratchpad.role;

grReqItem.variables.branch_suite_device = 'No';

grReqItem.variables.comments_box = workflow.scratchpad.comments;

grReqItem.parent = current.sys_id;


grReqItem.update();

}

 

 

If you have a switch statement at the beginning of the second workflow then you must add a 5 second timer to allow the switch statement to receive the value from the parent request.   

 

mak1A4
Tera Guru

If you're looking for a way to achieve adding a new catalog item to an existing request with the CartJS API have a look at this Article:

https://community.servicenow.com/community?id=community_article&sys_id=0585e0a71bc98554cdd555fa234bc...

Jide4
Giga Contributor

Hi Kristy, I noticed there is a second and third RITM created after following your steps. How do I remove the second RITM that only have variables form the Original request copied over?

A Snippet attached.

RITM ending in 7180 is the original requested item

The one ending 7181 has the values in the original copied over with no flow context associated with it

The one ending 7182 is theactual 2nd RITM with all the details and flow associated.

 

Kindly provide your input.