How to get Base64 content of all attachments on Incident?

peterraeves
Mega Guru

I have been reading about 'GlideSysAttachment.getBytes()', but it started to confuse me. As far as I understand it should be called like this:

var sa = new GlideSysAttachment();

var bytes = sa.getBytes(attachment.getValue("table_name"), attachment.getValue("table_sys_id"));

var content64 = GlideStringUtil.base64Encode(bytes);

What confuses me is that you have to use 'table_sys_id'. That you have to use the sys id of the incident instead of the attachment. So how do you differentiate between different attachments? What if you need to get all attachments on an Incident, how would you get the second attachment? I tried to replace 'table_sys_id' with 'sys_id', but that returned empty bytes...

14 REPLIES 14

peterraeves
Mega Guru

So I tried 'GlideSysAttachment.get()'. This did not work either, as it seems to only work for text based files.



var sa = new GlideSysAttachment();


var bytes = sa.get(gliderecord, filenameWithoutExtension);


var content64 = GlideStringUtil.base64Encode(bytes);



When entering a pdf for example, 'get' returns null.


try this ...


var att = new GlideRecord('sys_attachment');


att.addQuery('table_name','incident');


att.addQuery('table_sys_id',sys id of the record);


att.query();


var sa = new GlideSysAttachment();


var data = sa.getBytes(att);


while(att.next())


{


var sa = GlideSysAttachment();


  var binData = sa.getBytes(att);


  var encData = GlideStringUtil.base64Encode(binData); // data of one attachment at a time


}


Almost there. That code seems to be getting the data correctly, but it throws an exception. So if we can solve this we found a solution!


Stream closed: java.io.IOException: Stream closed: java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)


java.io.BufferedInputStream.fill(BufferedInputStream.java:246)


java.io.BufferedInputStream.read(BufferedInputStream.java:265)


com.glide.ui.SysAttachmentInputStream.writeTo(SysAttachmentInputStream.java:162)


com.glide.ui.SysAttachment.getBytesUnsafe(SysAttachment.java:656)


com.glide.ui.SysAttachment.getBytes(SysAttachment.java:619)


sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)


sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)


java.lang.reflect.Method.invoke(Method.java:498)


org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:256)


org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1227)


org.mozilla.javascript.gen.c12136.call(null.null:6)


org.mozilla.javascript.gen.c12136.exec(null.null)


com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:233)


com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:105)


com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:72)


com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:318)


com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:283)


com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:254)


com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:242)


com.glide.processors.ScriptProcessor.evaluateScript(ScriptProcessor.java:287)


com.glide.processors.ScriptProcessor.runScript(ScriptProcessor.java:206)


com.glide.processors.ScriptProcessor.process(ScriptProcessor.java:167)


com.glide.processors.AProcessor.runProcessor(AProcessor.java:409)


com.glide.processors.AProcessor.processTransaction(AProcessor.java:183)


com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:165)


com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:32)


com.glide.sys.ServletTransaction.run(ServletTransaction.java:34)


java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)


java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)


java.lang.Thread.run(Thread.java:745)


btw, what's the point of line 6? 'var data = sa.getBytes(att);'