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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

sample script below; it will work; forgot to add the user_info key

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;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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

 

- Pradeep Sharma

Hi Ankur,

I'm also having the same scenario, Here i can get description value but not able to get the  (value":"euhubzpa102") any help on this one.

{"schemaId":"AzureMonitorMetricAlert","data":{"version":"2.0","properties":null,"status":"Activated","context":{"timestamp":"2021-10-12T09:25:21.7021523Z","id":"/subscriptions/933c2f10-9eff-49d0-a490-60ce1c3a0c72/resourceGroups/EU-HUB-PRD-AUT-RG001/providers/microsoft.insights/metricAlerts/EU-MON-LINUX-DISK-WARNING-ALERT01","name":"EU-MON-LINUX-DISK-WARNING-ALERT01","description":"This is a warning alerts describes that one of the linux server instance used space is more than 90%.","conditionType":"SingleResourceMultipleMetricCriteria","severity":"3","condition":{"windowSize":"PT30M","allOf":[{"metricName":"Average_% Used Space","metricNamespace":"Microsoft.OperationalInsights/workspaces","operator":"GreaterThan","threshold":"90","timeAggregation":"Average","dimensions":[{"name":"Computer","value":"euhubzpa102"},{"name":"InstanceName","value":"/"}],"metricValue":91,"webTestName":null}]},"subscriptionId":"933c2f10-9eff-49d0-a490-60ce1c3a0c72","resourceGroupName":"eu-hub-prd-aut-rg001","resourceName":"EU-HUB-MON-LAWS-01","resourceType":"Microsoft.OperationalInsights/workspaces","resourceId":"/subscriptions/933c2f10-9eff-49d0-a490-60ce1c3a0c72/resourceGroups/eu-hub-prd-aut-rg001/providers/Microsoft.OperationalInsights/workspaces/EU-HUB-MON-LAWS-01","portalLink":"https://portal.azure.com/#resource/subscriptions/933c2f10-9eff-49d0-a490-60ce1c3a0c72/resourceGroups/eu-hub-prd-aut-rg001/providers/Microsoft.OperationalInsights/workspaces/EU-HUB-MON-LAWS-01"}}}

 

Hi Ankur,

Can you please help me why we used parser.decode(reqbody); 

can we use encode inplace of decode. Please help me the difference between between encode and decode

Regards,

Anuhsa

 

@Ankur Bawiskar  for visibility