I have a scripted REST API to create REQ/RITM of a particular catalog, how do I modify REST Request Body before I create REQ?

djd
Mega Contributor

I have a scripted REST API to create REQ/RITM of a particular catalog, how do I modify REST   Request Body before I create REQ?

7 REPLIES 7

djd
Mega Contributor

Hey Chuck,



I have a catalog item which has Variable Set with fields like (Requested_For, Phone, Line manager, cost center )....the third party tool will send the "name" of "Requested for".


Eg..


{


"sysparm_quantity":"1",


"variables":{


"s_name":"PhotoShop",


"e_ref_num":"SOF1234",


"requested_by":"Daileena Gomes",


"requested_for":"Joel Dsouza",


"phone_number":""


        }


}



and in scripted rest API I am pulling Requested for SYS_ID and phone , line manger details from SYS_USER and want to set this values in REQUEST BODY...and then hit "cart.orderNow(request_body);"



currently the Scripted REST creates RITM and populated VARIABLES , however the REQUESTED FOR and REQUESTED BY will get populated only if I pass SYS_ID.



I am using code in "Buy Item " resource of Service Catalog REST API as baseline.



If not possible in Scripted REST, I am thinking of setting it once REQ is created via Business Rule, however need to check various ways I can do this and then use the best one.


akunk23
Kilo Contributor

Did you ever get this figured out DJD? We are running into a similar situation.


djd
Mega Contributor

Hey,



you can access incoming message body using below



var request_body = request.body.nextEntry();


var requestVar = request_body.variables;




Then, you can modify variable values in cart using below.



var cartId = GlideGuid.generate(null);


var cart = new Cart(cartId);


var item = cart.addItem(itemId);



cart.setVariable(item, "requested_by", req_by);


cart.setVariable(item, "requested_for", req_for);


cart.setVariable(item, "line_manager", requestedForDetails[0]);


cart.setVariable(item, "cost_center", requestedForDetails[2]);//not working not sure why


cart.setVariable(item, "phone_number", requestedForDetails[1]);


cart.setVariable(item, "location", requestedForDetails[3]);



hope this helps.


Regards