- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 12:16 AM
Happy new year!
I want to add a header to a scheduled report (csv). I built an onAfter BR on sys_email table to read the attachment and create a new one:
var tableName = current.getTableName();
var sysId = current.sys_id;
StringUtil = GlideStringUtil;
var sa = new GlideSysAttachment();
var bytesContent = sa.getBytes(tableName, sysId);
var strData = String(Packages.java.lang.String(bytesContent));
...
var newData = header + '\n' + strData;
var att = new Attachment();
var msg = att.write(tableName, sysId, file, type, newData);
This is functioning well except the (German) special characters like ä, ö, ü - it will be displayed as something like this: ï ¿ ½ or a replacement character.
By the way: the original attachment correctly displays all the special characters.
Any help to correctly decode/encode would be appreciated.
Peter
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 12:45 AM
Hi Peter,
Following is the code to encode/decode the attachment content
var StringUtil = Packages.com.glide.util.StringUtil;
var attachmentSysId = ''; // sys_id of the attachment record in sys_attachment table
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', attachmentSysId);
gr.query();
if(gr.next()){
var sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(gr);
var encData = StringUtil.base64Encode(binData);
gs.log("Encoded data for attachment is:"+encData);
var decodedData = StringUtil.base64Encode(encData);
gs.log("Decoded data for attachment is:"+decodedData);
}
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
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
‎01-11-2017 12:45 AM
Hi Peter,
Following is the code to encode/decode the attachment content
var StringUtil = Packages.com.glide.util.StringUtil;
var attachmentSysId = ''; // sys_id of the attachment record in sys_attachment table
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', attachmentSysId);
gr.query();
if(gr.next()){
var sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(gr);
var encData = StringUtil.base64Encode(binData);
gs.log("Encoded data for attachment is:"+encData);
var decodedData = StringUtil.base64Encode(encData);
gs.log("Decoded data for attachment is:"+decodedData);
}
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
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
‎01-11-2017 01:07 AM
Hello Ankur,
I get a "Evaluator: java.lang.SecurityException: Illegal attempt to access class 'com.glide.ui.SysAttachment' via script
Caused by error in script at line .." using your code?
Thx, Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 01:19 AM
Hi Peter,
Instead of this Packages.com.glide.ui.SysAttachment()
use GlideSysAttachment()
That is the class as replacement of the package call.
If this line also gives error Packages.com.glide.util.StringUtil; then use GlideStringUtil() class
Here is the link for the package replacement calls
http://wiki.servicenow.com/index.php?title=Packages_Call_Replacement_Script_Objects#gsc.tab=0
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
‎01-11-2017 01:18 AM
Hello,
one more finding: if I add some special characters to the header (not decoded before) I get the same error - it seems it's more a problem of writing than decoding, isn't it?
Peter