Unable to parse result from response body

clyon
Tera Guru

I have a REST message. I'm getting the data back in the response (see below). But when I try to parse the fields out so I can update the fields on my form, I get undefined.

What am I missing?

var response = restMessage.execute();

gs.log(response.getBody());

//{"result":[{"u_segment":"Regional","u_account_owner":"Denise Bond","name":"123 MONEY LLC","u_region":"NE"}]}

 

var str = response.getBody();

var parser = new JSONParser();  

//var parsedResponse = response.parser(str);  

var parsedResponse = parser.parse(str);

gs.log(parsedResponse.result.name);

 

//Set values returned to incident form

current.u_account_name = parsedResponse.result.name;

current.u_account_owner = parsedResponse.result.u_account_owner;

current.u_region = parsedResponse.result.u_region;

current.u_segment = parsedResponse.result.u_segment;

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Christina,



Following script will give you the desired values:


You missed the array of object. the values are present in result[0]



var response = restMessage.execute();


var str = response.getBody();


var parser = new JSONParser();


var parsedData = parser.parse(str);


current.u_account_name = parsedData.result[0].name;


current.u_account_owner = parsedData.result[0].u_account_owner;


current.u_region = parsedData.result[0].u_region;


current.u_segment = parsedData.result[0].u_segment;



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

17 REPLIES 17

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Christina,



Following script will give you the desired values:


You missed the array of object. the values are present in result[0]



var response = restMessage.execute();


var str = response.getBody();


var parser = new JSONParser();


var parsedData = parser.parse(str);


current.u_account_name = parsedData.result[0].name;


current.u_account_owner = parsedData.result[0].u_account_owner;


current.u_region = parsedData.result[0].u_region;


current.u_segment = parsedData.result[0].u_segment;



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

That did it!! Thank you



One follow up question… I have hard coded a query parameter. How do I switch out the sysparam_query value for the variable 'org'?



TIA



var org = current.u_parent_org_id;



if (org != '') {


// Add your code here


var restMessage = new sn_ws.RESTMessageV2();


//restMessage.setBasicAuth('admin', 'removed so don't see - Tina knows!');


restMessage.setBasicAuth('rest_admin', 'r3stgnw!');


restMessage.setEndpoint('https://gmicustsvc.service-now.com/api/now/table/customer_account?sysparm_query=u_parent_org_id%3DB22222797Q&sysparm_fields=name%2Cu_account_owner%2Cu_region%2Cu_segment&sysparm_limit=1');


restMessage.setHttpMethod('GET');


restMessage.setRequestHeader("Accept","application/json");


restMessage.setRequestHeader('Content-Type','application/json');


// restMessage.setRequestBody(requestBody);


//


var response = restMessage.execute();


Glad that it worked.



One thing to your follow up question. I was not able to find the variable "org" anywhere in the endpoint. For dynamic values in the query or endpoint you can use variable substitutions in your rest message function.



https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/integrate/outbound_rest/concept/c...


http://wiki.servicenow.com/index.php?title=Outbound_REST_Web_Service#Variable_Substitution



At which place you are referring this.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Thank you for your help... I got it...



restMessage.setEndpoint('https://gmicustsvc.service-now.com/api/now/table/customer_account?sysparm_query=u_parent_org_id%3D${org}&sysparm_fields=name%2Cu_account_owner%2Cu_region%2Cu_segment&sysparm_limit=1');


Hi Christina,



Any update on this?


Can you mark my answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.



Regards


Ankur


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