The CreatorCon Call for Content is officially open! Get started here.

Retrieve value from Response.getBody() in JSON

manish123
Giga Guru

Hi All,

In REST integration, we are getting Response.getBody() with lots of fields and values respective to it. However, i would like to get some specific value from that Response.Could anyone please let me know how to get that? I tried with decode and Parse but still in log its displaying undefined.

1 ACCEPTED SOLUTION

Result does not appear to be an array. I'm reaching this as:



gs.log('Number=' + jsonObject.result.number);


View solution in original post

42 REPLIES 42

Were you able to do a successful PUT operation using the REST API Explorer? That helps eliminate a lot of variables and test the connection, credentials, and endpoint before you get in to scripts, events, etc.


manish123
Giga Guru

Hi Chuck,



Yes I was able to do a successful PUT operation using the REST API Explorer however here the endpoint is of same instance i.e. instancea but i would like to pass this work note change to instanceb. And while clicking on ServiceNow Script, it giving below line of codes:-


var request = new sn_ws.RESTMessageV2();


request.setEndpoint('https://instancea.service-now.com/api/now/table/incident/5a3ba22237a95e88456842f643990e0e');


request.setHttpMethod('PUT');



//Eg. UserName="admin", Password="admin" for this code sample.


var user = 'admin';


var password = 'admin';



request.setBasicAuth(user,password);


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


request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"work_notes\":\"This is for Test\"}");


var response = request.execute();


gs.log(response.getBody());



Addition to that I can see that work note has been updated to the incident sys_id which i have passed in REST API. And in response I have got so many fields with its respective values as below:-



{
  "result": {
  "parent": {
  "link": "https://instanceq.service-now.com/api/now/table/task/5c9d8a0d37edd688456842f643990eef",
  "value": "5c9d8a0d37edd688456842f643990eef"
  },
  "u_glide_date_2": "",
  "watch_list": "",
  "other_costs": "0",
  "u_Test_reference": "TEST0038069",
  "upon_reject": "cancel",
  "sys_updated_on": "2016-10-04 13:44:20",
  "type": "Project",
  "approval_history": "",
  "work_notes": "This is for Test",
  "number": "INC0003701",
  "defect": "",
  "related_records": "",
  "labor_costs": "0",
  "u_closed": "2016-02-16",
  "state": "1",


.......................................


I'm not sure what to tell you at this point. You are getting a response back which means the instances are communicating effectively. If you put work notes in the payload, they SHOULD be getting updated on the remote record.


manish123
Giga Guru

Hi Chuck,



I am able to pass the update of work notes from one instancea to another instanceb however for PUT i have hard coded End point in HTTP method to one incident only of instanceb. Could you please let me know how do i pass the real time sys_id? In Instancea we are storing Incident number of instanceb. Any idea how to get sys_id reference for the end point for PUT


method.


Currently it is :- https://instanceb.service-now.com/api/now/table/incident/1661c0892b5ea600b407b3b219da150a defined in HTTP Method


Below is the code for work notes update:-


try


{


var r = new sn_ws.RESTMessageV2('Instancea', 'put');


  var jsonMsg = {};


  jsonMsg.work_notes= current.work_notes;


    var json = new JSON().encode(jsonMsg);


  r.setRequestBody(json);


var response = r.execute();


  }


catch(ex) {


var message = ex.getMessage();


}


If you want to use dynamic parameters on the URL, you'll need to pass the information as a parameter. E.g.



https://instanceb.service-now.com/api/now/table/incident/${incident_id}



Then your script needs to do a setParameter() call.