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

Hello Ankur,


using your code (replaced by GlideSysAttachment) I am not able to get readable output - unlike using String(Packages.java.lang.String(binData)) mentioned above.


Best Regards, Peter


Hi Peter,



The encoded data is base64 data which is not in readable format. The decoded data will be readable for small files such as txt file etc.



Regards


Ankur


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

peterfischer
Kilo Contributor

Hi Ankur,


only a little typo in your answer - it should be: var decodedData = StringUtil.base64Decode(encData);


Thx a lot


Peter


Hi Peter,



Got it. Thanks for catching that. Could you also mark the answer as helpful and hit like. Thanks in advance.



Regards


Ankur


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

VaranAwesomenow
Mega Sage

I tried running this in Utah and it doesnt allow us to access com.glide.util, is there a Utah compatible script ?

 

Completed: Event Management - Impact for service gr.fd567e434716991021eaf2e7536d43c0 in 0:00:00.036, next occurrence is 2023-04-24 10:41:02, sys_created_on: 2022-10-07 09:13:56, trigger_type: 1 (Repeat), priority: 100, run_time: 1969-12-31 22:00:11, repeat: 1970-01-01 00:00:19, previous next_action time: 2023-04-24 05:10:43.000 UTC, current next_action time: 2023-04-24 05:11:02.000 UTC, max_drift: null, queued on: 2023-04-24 05:10:43.120 UTC, execution time millis: 36, actual start time: 2023-04-24 05:10:43.158 UTC, actual end time: 2023-04-24 05:10:43.194 UTC
Security restricted: Attempted access to restricted class name com.glide.util
Evaluator.evaluateString() problem: java.lang.SecurityException: Illegal attempt to access class 'com.glide.util' via script: com.glide.sys.security.GlideSecurityChecks.throwSecurityException(GlideSecurityChecks.java:118) com.glide.sys.security.GlideSecurityChecks.checkVisibilityToScripts(GlideSecurityChecks.java:110) org.mozilla.javascript.NativeJavaPackage.getPkgProperty(NativeJavaPackage.java:126) org.mozilla.javascript.NativeJavaPackage.get(NativeJavaPackage.java:82) org.mozilla.javascript.ScriptableObject.getProperty(ScriptableObject.java:2097) org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1569) org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1562) org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1271) org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830) org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160) com.glide.caller.gen._refname_.call(Unknown Source) com.glide.script.ScriptCaller.call(ScriptCaller.java:18) org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159) org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:597) org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3573) org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:172) com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:397) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:209) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:137) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:128) com.glide.script.Evaluator.evaluateString(Evaluator.java:103) com.glide.update.UpdateManager2.testFixScript(UpdateManager2.java:856) com.snc.apps.ScriptFixXMLHttpProcessor$ScriptFixWorker.runFixScript(ScriptFixXMLHttpProcessor.java:167) com.snc.apps.ScriptFixXMLHttpProcessor$ScriptFixWorker.evaluateScript(ScriptFixXMLHttpProcessor.java:145) com.snc.apps.ScriptFixXMLHttpProcessor$ScriptFixWorker.startWork(ScriptFixXMLHttpProcessor.java:125) com.glide.worker.AbstractProgressWorker.startAndWait(AbstractProgressWorker.java:126) com.glide.worker.ProgressWorker.startAndWait(ProgressWorker.java:52) com.glide.worker.AbstractProgressWorker.start(AbstractProgressWorker.java:101) com.snc.apps.ScriptFixXMLHttpProcessor.process(ScriptFixXMLHttpProcessor.java:86) com.glide.processors.XMLHttpProcessor.processJavaAJAX(XMLHttpProcessor.java:168) com.glide.processors.XMLHttpProcessor.process(XMLHttpProcessor.java:121) com.glide.processors.AProcessor.runProcessor(AProcessor.java:625) com.glide.processors.AProcessor.processTransaction(AProcessor.java:277) com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:184) com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:172) com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:51) com.glide.sys.Transaction.run(Transaction.java:2501) com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:27) java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) java.base/java.lang.Thread.run(Thread.java:829)
GlideSession message was modified by sanitization. [message=Illegal attempt to access class 'com.glide.util' via script][sanitized=Illegal attempt to access class 'com.glide.util' via script]