Web Service JSON creating blank incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-30-2016 05:59 AM
Hi All,
I am using Web Service to pass incident from one instance A to another instance B. In source A I am using Outbound --> REST Message. Which has got HTTP Request with Accept & Content-Type and value as json. And four HTTP Methods are also defined & these are Delete, post, put & get. In Source Instance A, i have created a UI action and calling the script action based on that event. Below are the line of codes, which is creating incident in Target Instance B but with blank and I would like to pass few values from instance A to instance while posting new ticket and these are state, short description, description, Caller etc.
In Instance B, I have created System Web Service--> Inbound. However in Web Service Transform Maps there is no field mapping yet. Do i need to define between staging table's field and destination table's field?
In Script action below is code:-
createticket();
function createticket()
{
function jsonEncode(str) {
str = new JSON().encode(str);
return str.substring(1, str.length - 1);
}
json.state = current.state;
json.short_description = current.short_description;
json.description = current.description;
json.u_caller = current.u_raised_by; // In Instance B caller is called as Raised By
try {
var r = new sn_ws.RESTMessageV2('Fruition API', 'post');
r.setStringParameter("short_description",current.short_description);
r.setStringParameter("state",current.state);
r.setStringParameter("description",current.description + '');
r.setStringParameter("u_caller",current.u_raised_by);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Response Body :' + responseBody);
}
catch(ex) {
var message = ex.getMessage();
}
}
In Log Response Body is printing:-
Response Body :{"result":{"number":"INC003813","close_notes":"","service_offering":"","u_total_hours_worked":"","u_project":"","additional_assignee_list":"","impact":"3","urgency":"3","correlation_id":"","u_business_service":"","u_update_flag":"true","description":"","state":"-6",,"short_description":"",,"comments_and_work_notes":","sys_created_on":"2016-09-30 10:15:11","reassignment_count":"0","u_symptom":"","u_estimated_cost":"0","opened_at":"2016-09-30,"sys_created_by":"abcd""}}
And in Source Instance A, in HTTP method Post, I have added Number and value as ${number} under section HTTP Query Parameters. And clicked on Test. It brings new incident number from instance B. And getting HTTP status as 201 However, when i have added one more parameter Short Description to it and Test that. Status code is returning 500
Error executing REST request: Invalid uri 'https://instanceb.service-now.com/api/now/table/incident?Number=&Short Description=': Invalid query
Could anyone of you please let me know what & where I would need to correct? Quick response must be appreciated

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2016 06:42 AM
You seem to have a lot of mixed things going on here.
You have defined the function jsonEncode, but you're not using it anywhere.
You're trying to set properties for some undefined Object called json on these lines: json.state = current.state; Your script will be breaking here.
But you don't need to define any JSON here if you're using setStringParameter(). The JSON should be in the Content of your post record.
This is what I think you want to do:
function createticket() {
var obj = {
"state":String(current.state),
"short_description":String(current.short_description),
"description":String(current.description),
"u_caller":String(current.u_raised_by)
};
var json = JSON.stringify(obj);
try {
var r = new sn_ws.RESTMessageV2('Fruition API', 'post');
r.setRequestBody(json);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Response Body :' + responseBody);
gs.log('Request: ' + r.getRequestBody()); // see what was sent
}
catch(ex) {
var message = ex.getMessage();
}
}
But there is another way you can do it.
You can define your request body in the Content field on the post HTTP Method record. In the content you define placeholders for your parameters using ${parameter}.
So your Content field would have this in it:
{"state":"${state}","short_description":"${short_description}","description":"${description}","u_caller":"${u_caller}"};
Then your script would be:
function createticket() {
try {
var r = new sn_ws.RESTMessageV2('Fruition API', 'post');
r.setStringParameter("short_description",current.short_description);
r.setStringParameter("state",current.state);
r.setStringParameter("description",current.description);
r.setStringParameter("u_caller",current.u_raised_by);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Response Body :' + responseBody);
gs.log('request: ' + r.getRequestBody()); // what was sent
}
catch(ex) {
var message = ex.getMessage();
}
}
Notice the main difference is setRequestBody(json) is used when you want to specify the entire body.
And setStringParameter(str) is used when you have the JSON "template" defined on your post record.
If you're sending the requests to an Import Set table on the other instance, then you definitely need to have a Transform Map set up. Otherwise you will end up with blank values. You will also need to update your JSON request body to reflect the field names on the staging table (not the names on the Incident table). So it might be {"u_state":"${state}"} etc.
If you are post directly into the Incident table, then you do not need a Transform Map.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2016 08:28 AM
Hi All,
We have configured web services between two SN instances and through this integration we are creating incident from instance A to another instance B. And Updating incidents to both sides. These parts are working perfectly.
However now the issue is for the configuration of attachments. And it should be from both sides during update. So, whenever any file is attached in instance A while raising the incident, the same would need to pass to other instance i.e. B's incident. Also after creation of incident if any file will get attached then the same to be passed to other instance and this part will be vice versa, means from instance A to B and instance B to A.
Could you please share the steps from starting or any URL from where I'll explore and do the proper setup for attachment in JSON.
Any help will be much appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2016 04:40 PM
Hi G N,
We are also trying to implement Bi-directional integration between two servicenow instances using Rest web service. Can you please share your update set or code for the integration. Any help would be appreciated.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 06:01 AM
Hi Sachin,