
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-01-2020 09:17 PM
Hello All,
I am building bidirectional integration of service now to zendesk using REST API.I created REST message for consuming REST API, setup token based authentication and rest of configuration is completed.
I need to consume zendesk REST API in servicenow.
Issue is zendesk expects nested JSON request body like below.
{
"ticket": {
"comment": { "body": "sachin chofosing dddAcme Jet Motors.", "public": true },
"status": "solved"
}
}
In above example, comment and status will be dynamic from incident record.
Comment body from above example is incident record comments and status will be incident state values.
How do i build this nested JSON request body to consume external REST API? Can someone give me sample code to build this kind of nested JSON payload?
I am using below REST API methods to consume external REST API.
Regards,
Sachin
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-02-2020 04:28 AM
you can form the json object in your script and then use setRequestBody() to set the json string in the body of POST/PUT method
var finalObj = {};
var commentObj = {};
commentObj['body'] = 'sachin chofosing dddAcme Jet Motors.';
commentObj['public'] = 'true';
var obj = {};
obj['comment'] = commentObj;
obj['status'] = 'solved';
finalObj['ticket'] = obj;
gs.info(JSON.stringify(finalObj)); // this would give you your nested json
var request = new sn_ws.RESTMessageV2();
request.setRequestBody(JSON.stringify(finalObj));
If my answer solved your issue, please mark my answer as ā Correct & šHelpful based on the Impact.
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-02-2020 03:51 AM
Hello,
here is some sample code we use to send a nested json body in an api call:
var body = {
"subject":"PC Replacement",
"start":{
"dateTime":start_dt,
"timeZone":"UTC"
},
"end":{
"dateTime":end_dt,
"timeZone":"UTC"
},
"location":{
"displayName":location.toString()
},
"body":
{
"contentType":"HTML",
"content":getInviteContent()
},
"attendees":[
{
"emailAddress":{
"address":email.toString(),
"name":name.toString()
},
"type":"required"
}
],
"categories":[
"Orange Category"
],
};
To send it in the api call,we then use
sendInviteRest.setRequestBody(JSON.stringify(body));
in your RESTMessageV2 definition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-02-2020 04:28 AM
you can form the json object in your script and then use setRequestBody() to set the json string in the body of POST/PUT method
var finalObj = {};
var commentObj = {};
commentObj['body'] = 'sachin chofosing dddAcme Jet Motors.';
commentObj['public'] = 'true';
var obj = {};
obj['comment'] = commentObj;
obj['status'] = 'solved';
finalObj['ticket'] = obj;
gs.info(JSON.stringify(finalObj)); // this would give you your nested json
var request = new sn_ws.RESTMessageV2();
request.setRequestBody(JSON.stringify(finalObj));
If my answer solved your issue, please mark my answer as ā Correct & šHelpful based on the Impact.
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-21-2020 11:21 AM
Hello Ankur / Team,
Greetings for the day!. I am also trying to integrate ServiceNow with Zendesk and want to update comments from Servicenow to Zendesk ticket using PUT method.In the above solution I am trying to pass current record's work_notes/ additional comments dynamically to the request body.
this is what I tried: commentObj.body = current.work_notes; (tried with comments_and_work_notes column/ other string fields too) but the line only accepts string that present in between"", in that case how to pass dynamic field value in the request body.
Could you please guide here!
Thanks in advance.
Regards,
Kalai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-21-2020 10:16 PM
Hi,
So you are consuming Zendesk API and want to send comment/work notes of current record.
I didn't get how to pass dynamic field value in request body.
you are already fetching the value using current object
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader