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 do we extract data from the response body and insert it into a field?

Reddy
Kilo Sage

Hello,

 

I am currently working on integrating ServiceNow with Asana. The purpose of this endeavor is to create a new project in Asana every time a new record is generated in ServiceNow. After successfully configuring the REST message, I am able to view the necessary values in the response body. However, I am now seeking assistance with the process of extracting the project ID (gid) from the response body and subsequently inserting it into the newly created record.

 

Reddy_1-1706801362206.png

 

Response: "gid":"1206496649022055","resource_type":"project"

 

 

 

responseBody{"data":{"gid":"1206496649022055","resource_type":"project","created_at":"2024-02-01T15:06:15.348Z","modified_at":"2024-02-01T15:06:16.039Z","members":[{"gid":"1203482360254605","resource_type":"user","name":""}],"owner":{"gid":"1203482360254605","resource_type":"user","name":""},"due_date":"2020-04-30","due_on":"2020-04-30","public":false,"name":"testing chargeback",

 

 

 

 

Business rule:

Reddy_0-1706802007642.png

 

 

 

 

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


var request = new sn_ws.RESTMessageV2 ('Asana Create Project', 'Create Project');
request.setStringParameterNoEscape('u_team_id', current.getValue('u_team_id'));
request.setStringParameterNoEscape('u_project_name', current.getValue('u_project_name'));
var response = request.execute();
var responseBody = response.getBody();
var statuscode = response.getStatusCode();
var errorMsg = response.getErrorMessage();

gs.info("responseBody"+responseBody);
})(current, previous);

 

 

 

 

1 ACCEPTED SOLUTION

DrewW
Mega Sage

It looks like a JSON formatted string so you should be able to just do this

var myvar = JSON.parse(responseBody);

//Then access things like this
myvar.data.grid

View solution in original post

1 REPLY 1

DrewW
Mega Sage

It looks like a JSON formatted string so you should be able to just do this

var myvar = JSON.parse(responseBody);

//Then access things like this
myvar.data.grid