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.

how to set requested for and opened by in a request using the cart API?

Ravish Shetty
Tera Guru

Hi all,

We get request details from an external system using web service.

These details include opened by and requested for.

I am creating requests using the cart API.

How do i set the requested for and opened by using these details?

Here is the script that i use

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem(gs.getProperty('jsb'));//963a960e4f28e200cf15c98e9310c7e4
cart.setVariable(item, 'request_name', request_name);
cart.setVariable(item, 'request_details', request_details);

cart.setVariable(item, 'opened_by_email', opened_by_email);
cart.setVariable(item, 'requested_for_email', requested_for_email);

var rc = cart.placeOrder();
9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Ravio,



Your have to query sc_request table to update fields. Please refer sample script.


var rc = cart.placeOrder();


//Then query for the request and edit


var parentRequest = new GlideRecord('sc_request');


  parentRequest.addQuery('number', rc.number);


  parentRequest.query();


  if (parentRequest.next()) {


  //set values here.


  parentRequest.update();


  }


Sure this is all fine, but unfortunately, after the request is placed, the approving manager has already been selected.   Changing the requested_for AFTER is no longer applicable.



Any ideas on how you can specify the requested_for in cart before the order is placed?



Thanks in advance.


Abhinay Erra
Giga Sage

Are you requested_for and opened _by form the web service? What details are you getting from web service?


vab_13
ServiceNow Employee
ServiceNow Employee

In order to change the requestedfor value on request, you can create an onbefore business rule on sc_request table



on before


Conditions: Item is <slect your item>


Script:



var gr= new GlideRecord("sc_request");


gr.get(current.getValue("request"));


gr.requested_for=current.variables.requested_for;


gr.update();