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 retrieve value from requestBody in scripted rest

sameer S
Tera Contributor

Hi , I m working on an integration in which third party tool is creating record in servicenow instance. Hence we are reading the values in scripted rest and doing the insert. The requestData has some key value pair in nested json format . e.g  below. How do I fetch the values of requested_for and requester_id separately so I could insert them to their respective columns in the record?  Please advise.

json body


"number":"SCTASK0010157",
"description":"test",
"user_info":
{"requested_for":"sameer s", "requester_id" :"001"}

 

scripted rest

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body;
var requestData = requestBody.data;
var taskNumber = requestData .number;
var desc = requestData.description;
var user_information = requestData.user_info;
var n = user_information.requested_for; // not working
var m = requestData["user_information"]["requester_id"]; // not working
})(request, response);

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

I lost correct on this one. Ankur's initial response had user_info key missing in the script 😉

 

- Pradeep Sharma

View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

 

Hello Sameer,

Minor modification..

var reqbody = request.body.dataString;

var parser = new global.JSON();
var parsedData = parser.decode(reqbody);

var requestedFor = parsedData['user_info'].requested_for;

var requester_id = parsedData['user_info'].requester_id;

 

- Pradeep Sharma

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

I lost correct on this one. Ankur's initial response had user_info key missing in the script 😉

 

- Pradeep Sharma