Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

JSON is getting Malformed on comments

stevecrocker
Kilo Expert

****Please not I am very new to this and have learned a lot but not a ton.   If someone knows could you offer a solution.   I can't figure this out.   Please explain like I'm 5 yrs old 😉

I have the following json script in my REST api message that looks like this

{

        "ticket":{

                      "id":"${id}",

                      "comment":"${comments}",

                        "custom_fields":

                            [{"id": "114102725251", "value":"${Snow_sys_id}"}]

                                  }

}

* I have the variable put in to servicenow.

The problem is the comments field it's a journalEntry.     When I just type a sentence in the description in the REST Message test, it works like a champ.

When I put this into the Business Rule which looks like this( see code below)   I get a 422 with the error messages following.

My question is how can I handle the crappy text that comes in through the description field using JSON?   is it possible to stringify the json in the api request

--Business Rule--

(function executeRule(current, previous /*null when async*/) {

try {

var r = new sn_ws.RESTMessageV2('Zendesk', 'ZD-Put');

r.setStringParameterNoEscape('comments', current.comments.getJournalEntry(1));

// r.setStringParameterNoEscape('Snow_sys_id', current.sys_id);

r.setStringParameterNoEscape('id', current.u_correlation_id);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

}

catch(ex) {

var message = ex.getMessage();

}

})(current, previous);

-----------------------------------------------------------------------------------------------------------------------------------------------------

--Error i'm getting from the Outbound http message--

--Response headers--

{Server=nginx, Date=Fri, 02 Feb 2018 17:02:15 GMT, Content-Type=application/json, Content-Length=72, Connection=keep-alive, X-Zendesk-Origin-Server=app38.pod13.usw2.zdsys.com, Set-Cookie=_zendesk_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiJWI2NDI3MDYwMmQ5MjJiNmRkZDVmM2U3ZDAxMDk2NWVlBjsAVEkiDGFjY291bnQGOwBGaQN0qAlJIgpyb3V0ZQY7AEZpA4wMCA%3D%3D--b974ade83561b10694e794f26927c1689d64923b; path=/; HttpOnly, X-Request-Id=05829b88-da28-4562-c50f-0a53bf0e4f26, X-Runtime=0.009493, X-Rack-Cache=invalidate, pass, X-Zendesk-Request-Id=be2b662132fbff642590}

Error message:

{"error":"Unprocessable Entity","message":"Server could not parse JSON"}   <--This is due to the current.comment.getJournalEntry(1) in the business rule has CR in it and it's messing with the formatting of the json.

3 REPLIES 3

VaranAwesomenow
Mega Sage

Dear Stephen,



The issue is because of newline character in the comments text. When we add comments to an incident it the text context of journal entry looks like this



2018-02-02 13:31:23 - System Administrator (Additional comments)
add comments



There is a new line character before add comments hence the JSON is not being parsed. I tried to convert that space to a string and it worked. I first get the comments journal entry to a variable and replace newline with space as highlighted in below code and it works.



This is my BR



(function executeRule(current, previous /*null when async*/) {




try {


var comment_text = current.comments.getJournalEntry(1);


comment_text = comment_text.replace(/[\r\n]+/g," ");


//var comment_text = "2018-02-02 13:31:23 - System Administrator (Additional comments)"   + " add comments";


var r = new sn_ws.RESTMessageV2('KingstonInstance', 'CreateIncident');


r.setStringParameterNoEscape('comments', comment_text);


//r.setStringParameterNoEscape('comments', current.comments.getJournalEntry(1));




// r.setStringParameterNoEscape('Snow_sys_id', current.sys_id);


r.setStringParameterNoEscape('worknotes', "Test BR WorkNotes");


gs.log(comment_text, 'varan');


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


}



catch(ex) {



var message = ex.getMessage();


gs.log(message, 'varan');



}



})(current, previous);



Thanks


Anil


jarodm
Mega Guru

If you control the code on the sending and receiving system, a quick fix would be to encode to base64 and send the encoded text. On the other end decode the string and the line breaks/etc. will all be in their proper locations.



Global example


var base64 = GlideStringUtil.base64Encode(commentField);


var str = GlideStringUtil.base64Decode(base64);



Scoped examples:


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_SGSYS-base64Decode_S