Use the link in REST Response object to fetch object details

hetalvadanlal
Tera Contributor

Hi,

I am currently using REST api for connecting two ServiceNow instances (e.g. SNow1 and SNow2).

The requirement is as follows:

1. An incident I1 in SNow1 is assigned to a particular group (SNow2 - Group)

2. Create a new incident I2 in SNow2 with details from SNow1

3. Response will contain details of new incident I2

4. Use details from Response for I2 to update references in I1

Step 1 and 2 are achieved already and working fine

For Step 3 and 4, the respose contains key:value mapping

The fields of I2 which are reference fields contains links in their response. (e.g.   "service_offering":{"link":"https://predev1bhpbio.service-now.com/api/now/v1/table/service_offering/f3ce2c3badcf5900837b9c9c7348...","value":"f3ce2c3badcf5900837b9c9c7348e4f9"})

I need to get the details of referred object.

I would like to know how i can achieve this particular requirement.

Regards

Hetal

1 ACCEPTED SOLUTION

hetalvadanlal
Tera Contributor

Hi,


I have achieved this by fetching the value from "link" attribute. Whenever I am encountering the value of a response field is an object, i do a separate processing


copyRespToObj: function(source, destination, type)


{


var mapDetails = new GlideRecord("u_mapping");


mapDetails.addQuery("u_type", type);


mapDetails.addQuery("u_active", true);


mapDetails.query();


while(mapDetails.next()){


if(source[mapDetails.u_source_column]!= ""){


if(typeof source[mapDetails.u_source_column] == "object"){


var obj = source[mapDetails.u_source_column];


this.ascLogger.debug("link = " + obj.link);


if(obj.link!=null){


var value = this.getDetails(obj.link);


if(value!=null){


destination.setDisplayValue(mapDetails.u_destination_column, value);


}


}


}else{


destination.setValue(mapDetails.u_destination_column, source[mapDetails.u_source_column]);


}


}


}


},



getDetails: function(link){


try{


var restMsgGet=new RESTMessage('ESMIncidents', 'get');


restMsgGet.setRestEndPoint(link);


var response= restMsgGet.execute();


var body = response.getBody();


var parser = new JSONParser();


var parsed = parser.parse(body);


return parsed.result.name;


}catch(e){


return null;


}


},


View solution in original post

1 REPLY 1

hetalvadanlal
Tera Contributor

Hi,


I have achieved this by fetching the value from "link" attribute. Whenever I am encountering the value of a response field is an object, i do a separate processing


copyRespToObj: function(source, destination, type)


{


var mapDetails = new GlideRecord("u_mapping");


mapDetails.addQuery("u_type", type);


mapDetails.addQuery("u_active", true);


mapDetails.query();


while(mapDetails.next()){


if(source[mapDetails.u_source_column]!= ""){


if(typeof source[mapDetails.u_source_column] == "object"){


var obj = source[mapDetails.u_source_column];


this.ascLogger.debug("link = " + obj.link);


if(obj.link!=null){


var value = this.getDetails(obj.link);


if(value!=null){


destination.setDisplayValue(mapDetails.u_destination_column, value);


}


}


}else{


destination.setValue(mapDetails.u_destination_column, source[mapDetails.u_source_column]);


}


}


}


},



getDetails: function(link){


try{


var restMsgGet=new RESTMessage('ESMIncidents', 'get');


restMsgGet.setRestEndPoint(link);


var response= restMsgGet.execute();


var body = response.getBody();


var parser = new JSONParser();


var parsed = parser.parse(body);


return parsed.result.name;


}catch(e){


return null;


}


},