Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

VaranAwesomenow
Mega Sage
// var encoded = global.JSUtil.btoa(text);
var base64string = GlideStringUtil.base64Encode("^.^ JOHN ANDERSEN!");
gs.info(base64string);
//yields: Xi5eIEpPSE4gQU5ERVJTRU4h

orig = GlideStringUtil.base64Decode(base64string);
//yields: ^.^ JOHN ANDERSEN!
gs.info(orig);