Multiline worknotes using REST API

Community Alums
Not applicable

Hi,

How to pass multiline worknotes without escaping newlines and special characters in REST API.

I am trying with below code.

var worknotes = current.work_notes.getJournalEntry(1).toString();
		worknotes = worknotes.replace(/[^a-zA-Z0-9\r\n ]+/g, " ");
r.setStringParameter('work_notes',new global.JSON().encode(worknotes));

Currently worknotes coming as below when I tested in postman. I need to send line by line as it is in worknotes.

Sample Response:

(Work notes)\nType of Call: outbound\r\nPC #: 3221\r\n\r\nCustomer's Complete Name and Position: Terra \r\nAlternate Callback #: 9191231\r\n\r\nIssue Reported by the Customer: POS connection error\r\nIssue start date and time:\r\n\r\n

Thank you in advance.

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try this

use the method -> setStringParameterNoEscape()

var worknotes = current.work_notes.getJournalEntry(1).toString();
worknotes = worknotes.replace(/[^a-zA-Z0-9\r\n ]+/g, " ");
r.setStringParameterNoEscape('work_notes',new global.JSON().encode(worknotes));

regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Community Alums
Not applicable

Hi Ankur,

Thanks for your reply.

But it is not working. I am receiving below error in logs.

Js body Infotmation is:[{"message":"Request cannot be parsed."}]

Thanks

Hi,

Can you share your complete script?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Community Alums
Not applicable

Hi Ankur,

Please find the code below.

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

	
	var state=current.incident_state;
	var pc=current.location.getDisplayValue();
	var parsedPc="DKN"+pc;
	
	var a=current.work_notes.getJournalEntry(1).toString();
	var c=a+" ";
	var e =c.replace(/\r\n|\r|\n|\\\\\\/g, '\\n');
	var b = e.replaceAll('|','');
	//var b=c.replace(/\r\n|\r|\n|\\\\\\/g, '\\n');
	var des=current.short_description+" ";

	var desParsed=des.replace(/\r\n|\r|\n|\\\\\\/g, '\\n');
		var destrim=desParsed.substring(0,69);
//gs.addInfoMessage("work_notes: "+b);
	gs.addInfoMessage(parsedPc);
	var r = new sn_ws.RESTMessageV2('Createe APP pwd', 'Create TKT');
 r.setStringParameterNoEscape('caller', parsedPc);
 r.setStringParameterNoEscape('processingStatus', 'Logged');
 r.setStringParameter('briefDescription',destrim);
 r.setStringParameterNoEscape('category', 'Kiosk Terminal');
 r.setStringParameterNoEscape('urgency', 'Digital Customer Experience');
	 r.setStringParameterNoEscape('subcategory', 'Software');
r.setStringParameter('work_notes',b);
 r.setStringParameterNoEscape('state', 'Logged');
 r.setStringParameterNoEscape('number', current.number);

 var response = r.execute();
 var httpStatus = response.getStatusCode();
	gs.log("http status is:"+httpStatus);
	var jsonBody =   response.getBody();
gs.log("Js body Infotmation is:"+jsonBody);
	

var jsonObject = JSON.parse(jsonBody);
	gs.log('Number=' + jsonObject.result.number);
	gs.log('Number=' + jsonObject.number);
	var Body=r.getRequestBody();
	gs.log("AC body"+Body);
	
	gs.addInfoMessage('Number=' + jsonObject.number);
	current.u_vendor_reference_number=jsonObject.number;
	current.update();
})(current, previous);


   Thanks