The CreatorCon Call for Content is officially open! Get started here.

Attachment encoding

peterfischer
Kilo Contributor

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

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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


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

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

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


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

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


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


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

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