Use existing request number with cart.placeOrder()

David247
Tera Contributor

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();
		
	}
});
10 REPLIES 10

Aman Kumar S
Kilo Patron

My humble request, don't do it!

Numbers are auto-generated and should not be manipulated. In this case even if you want to create duplicate that is fine, add the old ref in some new custom field.

Why I am suggesting against it, It will not be fruitful at all, since unique identifier is sys_id and and not number, even if you get the same RITM num, what purpose would it fulfill?

Best Regards
Aman Kumar

There are other requested items in the request from the order guide. The overall process is to delete the old requested item with the bad flow and create a duplicate RITM with the same request.

The alternative is to create the new RITM with a new request, change the request and other info on the new RITM, then delete the new Request. How is that better than just simply using the old request number with the new RITM?

So its a one time job to get rid of the bad records, and create a new copy of RITM and associate with already created Request, with updated workflow?

Tell me, if I understood that correctly?

Best Regards
Aman Kumar

Yes, that is correct. There are 16 requested items with flows that have an error, we have a fix for the flow and need to regenerate the RITM's under the same request numbers.