- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2014 11:35 PM
Hi,
We have a unique requirement to communicate create an incident from one ServiceNow instance to other ServiceNow instance
The issue we are facing is that there are some fields like work notes and description which have new line character.
Whenever there there is a new line being set using "setStringParameter("variable_name", 'variable value with new line')", the incident creation does not happen
Below is the script
var r = new RESTMessage('TestIncidents', 'post');
r.setStringParameter("short_description", current.short_description);
r.setStringParameter("u_affected_user", current.u_affected_user);
r.setStringParameter("location", current.location);
r.setStringParameter("impact", current.impact);
r.setStringParameter("urgency", current.urgency);
r.setStringParameter("description", current.description);
r.setStringParameter("work_notes", current.work_notes);
r.setStringParameter("u_category", current.u_category);
r.setStringParameter("u_classification_level_3", current.u_classification_level_3);
r.setStringParameter("service_offering", current.service_offering);
r.setStringParameter("cmdb_ci",current.cmdb_ci);
r.setStringParameter("u_requestor", current.u_requestor);
r.setStringParameter("u_preferred_contact_number", current.u_requestor);
//r.setStringParameter("work_notes", current.work_notes.getJournalEntry(-1).toString);
var responser = r.execute();
gs.log("getBody: " + responser.getBody());
gs.log("getStatusCode: " + responser.getStatusCode());
gs.log("getHeaders " + responser.getHeaders());
Below is the log
getBody:{"error":{"message":"Exception while reading request","detail":"Verify Request body and Content-type headers. Not able to parse request"},"status":"failure"}
getStatusCode: 400
getHeaders {Date=Mon, 29 Dec 2014 03:35:50 GMT, Transfer-Encoding=chunked, Set-Cookie=BIGipServerpool_predev1bhpbio=421822474.35646.0000; path=/, X-Cnection=close, Content-Type=application/json, Server=ServiceNow}
Any ideas or inputs are highly welcomed
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2014 08:18 AM
Hi Hetal,
Sorry about that. The quotes are added as a result of the JSON.encode call in the previous script. The REST message will add its own quotes with setStringParameter, hence the conflict. I added a jsonEncode function to the script below to handle both the JSON encoding and stripping the leading and trailing quotes that are added by the encode function:
var r = new RESTMessage('TestIncidents', 'post');
function jsonEncode(str) {
str = new JSON().encode(str);
return str.substring(1, str.length - 1);
}
r.setStringParameter("short_description", current.short_description);
r.setStringParameter("u_affected_user", current.u_affected_user);
r.setStringParameter("location", current.location);
r.setStringParameter("impact", current.impact);
r.setStringParameter("urgency", current.urgency);
r.setStringParameter("description", jsonEncode(current.description + ''));
r.setStringParameter("work_notes", jsonEncode(current.work_notes + ''));
r.setStringParameter("u_category", current.u_category);
r.setStringParameter("u_classification_level_3", current.u_classification_level_3);
r.setStringParameter("service_offering", current.service_offering);
r.setStringParameter("cmdb_ci",current.cmdb_ci);
r.setStringParameter("u_requestor", current.u_requestor);
r.setStringParameter("u_preferred_contact_number", current.u_requestor);
//r.setStringParameter("work_notes", current.work_notes.getJournalEntry(-1).toString);
var responser = r.execute();
gs.log("getBody: " + responser.getBody());
gs.log("getStatusCode: " + responser.getStatusCode());
gs.log("getHeaders " + responser.getHeaders());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2014 08:18 AM
Hi Hetal,
Sorry about that. The quotes are added as a result of the JSON.encode call in the previous script. The REST message will add its own quotes with setStringParameter, hence the conflict. I added a jsonEncode function to the script below to handle both the JSON encoding and stripping the leading and trailing quotes that are added by the encode function:
var r = new RESTMessage('TestIncidents', 'post');
function jsonEncode(str) {
str = new JSON().encode(str);
return str.substring(1, str.length - 1);
}
r.setStringParameter("short_description", current.short_description);
r.setStringParameter("u_affected_user", current.u_affected_user);
r.setStringParameter("location", current.location);
r.setStringParameter("impact", current.impact);
r.setStringParameter("urgency", current.urgency);
r.setStringParameter("description", jsonEncode(current.description + ''));
r.setStringParameter("work_notes", jsonEncode(current.work_notes + ''));
r.setStringParameter("u_category", current.u_category);
r.setStringParameter("u_classification_level_3", current.u_classification_level_3);
r.setStringParameter("service_offering", current.service_offering);
r.setStringParameter("cmdb_ci",current.cmdb_ci);
r.setStringParameter("u_requestor", current.u_requestor);
r.setStringParameter("u_preferred_contact_number", current.u_requestor);
//r.setStringParameter("work_notes", current.work_notes.getJournalEntry(-1).toString);
var responser = r.execute();
gs.log("getBody: " + responser.getBody());
gs.log("getStatusCode: " + responser.getStatusCode());
gs.log("getHeaders " + responser.getHeaders());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2014 08:32 AM
Hi Travis
It worked perfect!
Thank you so much for prompt responses
Regards
Hetal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2015 10:29 PM
Hi i have tried the above code, it was worked perfectly while getting response from REST. But while creating SOAP response it is not working.
My issue is While getting response from external system, Description filed will come with \n or \r characters. I have tried the JSON() code, it was worked for REST but not for SOAP.
if (!this._isEmpty(this.description)){
var xDesc1 = this.description;
xDesc = xDesc1.replace(/\n/g, '\n');
}
I have tried the above code, but it wont worked out.
Can anybody resolve this asap?
Thanks in Advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2015 02:48 AM
I tried with the below code. Its working now.
if (!this._isEmpty(this.description)){
xDesc = this.description.replace(/\\n|\\r\\n|\\r/gm, '\n');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2016 10:34 PM
Hi Travis,
We have used the above code but getting below error:
{"errorMessages":["Unrecognized character escape '&' (code 38)\n at [Source: org.apache.catalina.connector.CoyoteInputStream@71134d; line: 8, column: 240]"]}
Looks like it is not replacing the '&' character, Can you please help ?