Inbound REST, JSON as a field value

Jamsta1912
Tera Guru

Hello all,

I'm working on a system integration with a third party. I have created a web service and transform map in our DEV instance, and we're able to receive and process inbound POSTs with bodies like this:

{
"u_number":"INC7654521",
"u_location":"HQ",
"u_cmdb_ci":"Toaster"
}

However, the third party would like to standardise their outbound messages to have bodies like this:

{
"incident":{
"u_number":"INC7654521",
"u_location":"HQ",
"u_cmdb_ci":"Toaster"
}
}

So, the JSON in my first example itself becomes the value of a field called "incident" in my second example. This doesn't work for the web service I've created, which has field names 'u_number', 'u_location, and 'u_cmdb_ci', and I guess in the second example the message is 'looking for' a field named 'incident'. 

What are my options for processing the inbound message in the second case? I'm thinking I may have to write a script to place each inner field value onto the input row to trigger the transform map, but I would like to know what ya'll think.

Jamie.

1 ACCEPTED SOLUTION

You can construct the response body in the Scripted rest api script as below

 

var body = {};
body.name = "incident";
body.number = "1234";
body.caller = {"id": "user1"};
response.setBody(body);

reference: https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_re...

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

12 REPLIES 12

Yes. That's correct. For ex, below is how we are parsing the data sent by external app, and then loading it to the import set table. So you wont need to change the import set or transform map. You will just need to add the scripted rest api which will parse the inbound request and load it to your import set table.

 

var locationXMLLength = locationXMLObject.getNodes("/LOCATIONS/LOCATION").getLength();
//gs.info('locationXMLLength :'+locationXMLLength);
for(var k=1; k <= locationXMLLength; k++){
var locationGR = new GlideRecord('u_hcm_locations');
locationGR.initialize();
locationGR.u_active = true;
locationGR.u_city = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_CTY");
locationGR.u_zip = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_PSTL_CD");
locationGR.u_name = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_NM");
locationGR.u_location_id = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_CD");
locationGR.u_street = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_ADDR_LN");
locationGR.u_state = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_ST");
locationGR.u_country = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_CNTRY_NM");
// locationGR.u_full_name = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_FL_NM");
locationGR.u_full_name = locationXMLObject.getNodeText("/LOCATIONS/LOCATION["+k+"]/WRK_LOC_NM");
locationGR.insert();
}
var importSet = new GlideRecord('sys_import_set');
importSet.addQuery('table_name','u_imp_locations');
importSet.addQuery('state','loading');
importSet.query();
while(importSet.next()){
importSet.state = 'loaded';
importSet.update();
}


Please mark this response as correct or helpful if it assisted you with your question.

Thank you. It's good to know that I'm on the right track - and this is very useful :). I will let you know how I get on.

Jamie.

Hi again Sanjivmeher,

I've created a script that's succesfully adding the data to my import set and the data is being transformed to create an incident. This all works perfectly, so thank you again.

However, how would I construct a response to the original inbound message that contains the reference of the incident that has been generated?

Jamie

 

You can construct the response body in the Scripted rest api script as below

 

var body = {};
body.name = "incident";
body.number = "1234";
body.caller = {"id": "user1"};
response.setBody(body);

reference: https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_re...

Please mark this response as correct or helpful if it assisted you with your question.

Jamsta1912
Tera Guru

Thanks for sticking with me on this!

My issue really is how do I actually grab the incident reference and bring it back into the script?
The script runs and adds a record to the import set, the transform map then runs and the incident is created. Can I somehow pass the incident number back up to the script while it's still running? I could do a GlideRecord lookup within the script, but only after waiting for the incident to have been created - so I'm not clear on the best practice way to do this. Any thoughts?

Jamie.