How do I get a new line to not give an error in an outbound REST message

Wayne Richmond
Tera Guru

I have a outbound REST integration with a supplier. I am trying to pass Work Notes via the REST message, however, if there is a new line in the note, I get the following error in my logs:

{"error":{"message":"Exception while reading request","detail":"The payload is not valid JSON."},"status":"failure"}

 

I have tried replacing \n with \\n so it includes the \n in the payload, however, it doesn't seem to process this correctly. When I view my logs, it appears okay in the list view, but doesn't look right when I open the log record.

 

List view:

listview.png

 

Record view:

logview.png 

 

Here is my script that forms the request body:

var wnString = current.work_notes.toString().replace(/\n/g, '\\n');
    gs.info('XXXXX Problem Integration wnString: ' + wnString);

    var request = new sn_ws.RESTMessageV2('XX Problem', 'Update Problem');
    request.setStringParameterNoEscape('u_customer_reference', current.number);
    request.setStringParameterNoEscape('description', descString);
    request.setStringParameterNoEscape('short_description', current.short_description);
    request.setStringParameterNoEscape('priority', current.priority);
    request.setStringParameterNoEscape('state', current.state);
    request.setStringParameterNoEscape('rootcause', current.u_root_cause);
    request.setStringParameterNoEscape('workaround', current.work_around);
    request.setStringParameterNoEscape('comments', wnString);

 

How can I send Work Notes and other string fields that include new lines via the JSON request?

1 ACCEPTED SOLUTION

Absolutely! You can even use setRequestBody() while still using the "Update Problem" method. To do that all you need to do is clear the Content field on the record and then build your payload object in your script and pass it in as a JSON string. For example:

 

var payload = {
    u_customer_reference: customerVar, // etc.
}

var request = new sn_ws.RESTMessageV2('XX Problem', 'Update Problem');
request.setRequestBody(JSON.stringify(payload));
// etc.

 

If you're unfamiliar with the API Docs site I highly suggest checking it out. Here is a link to the docs for the RESTMessageV2 API that I think you'll find very helpful while working on this integration. I hope that helps!

View solution in original post

7 REPLIES 7

Dan Ostler
Tera Guru

Is there are reason you are using setStringParameterNoEscape() and not allowing character encoding from happening? Have you tried using setStringParameter()instead?

Hi Dan. Truth be told, I'm not an expert and am reusing code. 

I tried your suggestion but it didn't work:

request.setStringParameter('comments', current.work_notes);

 

Output:

didntwork.png

You can't obviously see the new lines in the request body.

I'm sorry, I should have asked this in my original response. Have you tested this and had success when using a comments value without a newline character? The reason I ask this is because I'm assuming that you have variables defined in the Content field of the "Update Problem" REST Method record, is that correct? Depending on the value of the Content field it is possible that is behind the invalid JSON.

 

Also, I apologize once again for not being more specific, I would still recommend including your regex conversion of \n to \\n. That is the way ServiceNow handles newline characters in the REST API Explorer. If you're unfamiliar with the REST API Explorer I recommend checking it out.

 

rest api explorer.png

 

 

Whenever substituting variables is involved rather than setting the body (using setRequestBody()) it can be hard to troubleshoot, especially without seeing where the variables are defined.

Thanks Dan. This is what the Content field looks like:

content.png

 

I 'minified' it to see if that made a difference, but it did not:

minified.png

 

I had a look at the REST API Explorer. I didn't realise it formatted the code to valid JSON like in your example. I looked at the ServiceNow Script Code Sample and saw it was using request.setRequestBody:

request.setRequestBody("{\"comments\":\"test\\\\n\"}")

 

Can I build the JSON payload this way, rather than calling the 'Update Problem' method?