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.

Cart API - Setting Request field

RichGK1
Mega Expert

I'm using the Cart API to create a cart and an item and subsequent request. This all gets created OK but when trying to set the special_instructions field on the request it does not get set.

Can anyone tell me what I am doing wrong?

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('b70cc309db2797804aba70600f961934');
var rc = cart.placeOrder(); // rc = request
gs.log("RGK - Created OK - number = " + rc.number);
gs.log("RGK - Created OK - requested_for = " + rc.requested_for);
	
//rc.special_instructions = current.description; // not getting set
rc.special_instructions = "12345"; // not setting
1 ACCEPTED SOLUTION

Cart runs on request. Special instructions is on the request table. Your gliderecord is also on request. So instead of using gliderecord, change your script adding below between var item and var rc:

var cartGR = cart.getCart()
cartGR.special_instructions = current.description;
cartGR.update();

View solution in original post

5 REPLIES 5

Michael Fry1
Kilo Patron

After your placeOrder, you can use a GlideRecord to set other fields. Something like this:

var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if(ritmRec.next()){
	ritmRec.special_instructions = '12345';
}

I'm still having a problem with this it turns out.

Here's my code. According to the log output grRequest.special_instructions is getting set OK but when I go to the request and check special_instructions it is blank.

I understand that we are not supposed to use update() within a business rule so how can I make the value stick?

(function executeRule(current, previous /*null when async*/) {

	var cartId = GlideGuid.generate(null);
	var cart = new Cart(cartId);
	var item = cart.addItem('b70cc309db2797804aba70600f961934');
	var rc = cart.placeOrder(); // rc = request object

	gs.log("RGK - Created OK - number = " + rc.number);
	
	var count = 0;
	
	var grRequest = new GlideRecord("sc_request");
	grRequest.addQuery("sys_id", rc.sys_id);
	grRequest.query();
	
	while(grRequest.next()){
		count++;
		grRequest.special_instructions = current.description;
		gs.log("RGK - 111" + current.description); // OK
		gs.log("RGK - 222" + grRequest.special_instructions); // OK
	}
	
	gs.log("RGK - Count = " + count);
	
})(current, previous);

Cart runs on request. Special instructions is on the request table. Your gliderecord is also on request. So instead of using gliderecord, change your script adding below between var item and var rc:

var cartGR = cart.getCart()
cartGR.special_instructions = current.description;
cartGR.update();

Thanks I've got it sorted now