We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to escape special characters

surya123
Mega Guru

Hi All,

I am sending the json to 3rd party system. So using the BR i was forming it this way -

 

var r = new sn_ws.RESTMessageV2('Creation', 'Default PUT');

r.setStringParameterNoEscape('adhocID', current.number);
r.setStringParameterNoEscape('fieldid', current.field_id);
r.setStringParameterNoEscape('state', stateVal);
r.setStringParameterNoEscape('worknote', current.work_notes));
r.setStringParameterNoEscape('group', current.assignment_group.getDisplayValue());
r.setStringParameterNoEscape('closed', current.closed_at);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

 

I get an error message as the "Record was misformed" when the worknote is typed as-

 

ASU, Test
testasu
CN=tyty\, fgt,OU=CGS Users,DC=tester,DC=cvs
Wednesday, December 27, 2017 12:19:12 PM

How can i escape the speacial characters or any issue with this and get it working?

Thanks

Surya

18 REPLIES 18

I see the log as this -

"<>?,./;'[]=-1234567890-\ndfygdhgg\n\\bddmd\n//\n"

 

But i have already entered more worknotes and log of notes show all the added data. Not sure what is going wrong. This my entire scrpt

function jsonEncode(str) {
var parser = new JSONParser();
var parsed = parser.parse(str);
var json = new global.JSON().encode(parsed);
gs.log(" Test" + json);
return json;
}

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

try {

var notes='';
var gr = new GlideRecord('sys_journal_field');
gr.addEncodedQuery("element=comments^ORelement=work_notes");
gr.addQuery('element_id',current.sys_id);
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
notes +=((gr.sys_created_on.toString() + ' - ' + gr.sys_created_by.toString() + ' ' + gr.element.toString() + '- ' + gr.value.toString() + "<br>").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));


}
//notes = notes.toString();
gs.addInfoMessage(notes);
var json = new JSON();
var r = new sn_ws.RESTMessageV2('Creation', 'Default PUT');
r.setStringParameter('ID', current.number);
r.setStringParameter('worknote',jsonEncode(notes));


var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();


}

surya123
Mega Guru

Hi,

I will need to encode the string of worknotes and set that in string paramter right. I dont need to decode it?

try changing below

notes +=(gr.sys_created_on + ' - ' + gr.sys_created_by + ' ' + gr.element + '- ' + gr.value + '\n');

and change

var json= new global.JSON();


json.encode(notes);

I believe the encoding simply converts the carriage returns (CR), line feeds (LF) into \r and \n. You don't need to decode them. The destination where they will be displayed (say a UI in a browser) should automatically decode and display them.

The below Community article may help -
https://community.servicenow.com/community?id=community_question&sys_id=478ecb21dbdcdbc01dcaf3231f961924&anchor=answer_90614431db105fc01dcaf3231f961902&view_source=searchResult