Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

What about deleting the old flow context and running something like this to create a new workflow for the original requested item? I tried this after deleting the old flow context and feeding that RITM info and a new flow was not created.

I tried both of these, the first one runs without error but no flow is created. The 2nd one has an error.

var grScReq = new GlideRecord('sc_req_item'); 
if(grScReq.get('number', 'RITM0231141')){
	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);
	}
}

var grScReq = new GlideRecord('sc_req_item'); 
if(grScReq.get('number', 'RITM0231141')){
	try { 
          var inputs = {}; 
          inputs['current'] = grScReq; // GlideRecord of table: 
          inputs['table_name'] = 'sc_req_item'; 

          // Starts the flow asynchronously.
          var contextId = sn_fd.FlowAPI.startFlow('global.hardware_request', inputs); 

    } catch (ex) { 
          var message = ex.getMessage(); 
          gs.error(message); 
    }
}	

The second query throws this error:

** Script: The inputs provided to the flow: Hardware Request within application scope: global are incorrect, please refer to the definition: no thrown error