How to remove all Line Feed (\n) and (\r) Carriage Return in the workflow script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2018 09:06 AM
Hello Everyone
Ihave a issue with which I am trying to deal with it. In my workflow "run script" activity I am extracting an attachment from the sys_attachment table (it is attached to the RITM) and I am encoding it to the base64 form because I need to send it in this way via web service and here is where the issue strats.
My file which is attached to the RITM is a simple host file in this format:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
10.1.14.14 abcd.local abcd deft # some comment
10.144.14.14 abcd.local abcd deft # some comment
After it is encoded to the base64 I am checking the workflow context to get the encoded value for it and then I am decoding it to check it the attachment was sent correctly. But I get a lot of strange characters, for example:
# This file contains the mappings of IP addresses to v7BW2V6Т2VG'6VB&RWBFfGV line. The IP address should
# be placed in the first covVfvVB'FR6'&W7Fr7BRТ2FRddress and the host name should be separated by at least vPТ276RТ0Т2FFFǒ6VG27V62FW6Z) may be inserted on individual
# lines or following ther6RRFVFVB'r2r7&Т0Т2f"WS
What I figure out is this is because SNOW after encoding adds the \n and \r and to get rid of them before send the soap message I am trying to remove them. My function look like:
function convertFile(){
var b64attachment = '';
var newb64EncodedText = '';
var rec = GlideRecord('sys_attachment');
rec.addQuery('table_name', 'sc_req_item');
rec.addQuery('table_sys_id', current.sys_id.toString());
rec.query();
if(rec.next()){
var sa = new GlideSysAttachment();
var binData = sa.getBytes(rec);
b64attachment = GlideBase64.encode(binData);
newb64EncodedText = b64attachment.toString().replace(/(\r\n|\n|\r)/gm,"").trim();
}
return newb64EncodedText;
}
After this operation I get an error which is: "Fault Description: org.mozilla.javascript.EvaluatorException: The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: class java.lang.String replace(char,char) class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence)"
Whats more if I try and use this function in Chrome console on the same encoded message it is removing all the \n and \r without any problem.
Can anyone of you faced similar issue and know how to remove it ?
Big thanks for any hints and answers 🙂
Best Regards
Mike
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2018 10:33 PM
Hi Guys
I found the solution for this issue 🙂
Instead of using this:
newb64EncodedText = b64attachment.toString().replace(/(\r\n|\n|\r)/gm,"").trim();
I used "java" based code which look like this:
newb64EncodedText = b64attachment.replaceAll("(\\r|\\n)", "");
So everything I do was to replace "replace" function with the "replaceAll" 🙂
Best Regards
Mike