Use existing request number with cart.placeOrder()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 05:46 AM
Greetings,
I need to be able to add a new request item to an existing request using cart.placeOrder().
My below code creates a new requested item, I just need to know how not to create a new request but use an old request number.
Thanks
var aryReqItems = ["RITM0231245", "RITM0231246"];
aryReqItems.forEach(function(item, index) {
var grReqItem = new GlideRecord("sc_req_item");
if (grReqItem.get("number", item)){
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var itemVar = cart.addItem("c26049d7db7fd740768834cc7c961918");
for (var varName in grReqItem.variables) { // Get and set all variable values from the old req item for the new one.
if (grReqItem.variables.hasOwnProperty(varName) ){
var varValue = grReqItem.variables[varName];
//gs.info(varName + ':' + variable);
cart.setVariable(itemVar, varName, varValue);
}
}
var rc = cart.placeOrder();
}
});
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 09:11 AM
Apologies, I misunderstood.
Did you try creating the RITM record and explicitly passing the function for initiating the flow for that RITM?
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 09:14 AM
Have a look at below code snippet for how to launch flow explicitly:
try {
var inputs = {};
inputs['table_name'] = 'Table Name';
inputs['request_item'] = ; // GlideRecord of table: sc_req_item
var result = sn_fd.FlowAPI.getRunner().flow('flow_internal_name').inBackground().withInputs(inputs).run();
var outputs = result.getOutputs();
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 09:48 AM
Hi Aman, thanks for the post. Is this supposed to restart the existing flow after the flaw is fixed? Here is my script which runs, but doesn't restart the flow.
var grScReq = new GlideRecord('sc_req_item');
if(grScReq.get('number', 'RITM0231246')){
try {
var inputs = {};
inputs['table_name'] = 'sc_req_item';
inputs['request_item'] = grScReq; // GlideRecord of table: sc_req_item
var result = sn_fd.FlowAPI.getRunner().flow('hardware_request').inBackground().withInputs(inputs).run();
var outputs = result.getOutputs();
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 11:06 AM
It doesn't restart the flow, but attach new flow context for the newly created RITM.
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 11:09 AM
I thinking the bad flows, that we discussed earlier should be explicitly cancelled/deleted as well, when you cancelling/deleting the RITMs.
Cancelling I know will have no reprucusions, but deleting might have some, please explore a lil bit around that, and share
For cancelling, I found something in HI:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0750702
Aman Kumar