Multiline worknotes using REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2022 01:46 AM
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:
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2022 03:29 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2022 11:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2022 10:27 PM
Hi,
Can you share your complete script?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 06:34 AM
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