Why is GlideSysAttachment.getContentBase64() documented, but not defined?

peterraeves
Mega Guru

I am pretty confused about this matter... I need to get the Base64 content of an attachment, so I went to the documentation and it recommends to use the function GlideSysAttachment.getContentBase64(): https://developer.servicenow.com/app.do#!/api_doc?v=geneva&id=r_SGSA-getContentBase64_GR

Though that function just does not exist in the system? It always keep popping up undefined...

When I run:

var sa = new GlideSysAttachment();

gs.print(typeof sa.getContentBase64);

var a = [];

for (var x in sa) {

      a.push(x);

}

a.sort();

gs.print('\n'+a.join('\n'));

I get the following output, which does not contain the function I need... So why is it not there, while it is in the documentation? I tried this on Geneva, Helsinki and Istanbul...

*** Script: undefined

*** Script:

DBImageAttachmentRecord

addEncryptionContexts

allAttachments

asStream

attachmentRecord

attachments

attachmentsForExtensions

bytes

bytesUnsafe

canRead

changeEncryptionContext

class

contentStream

contentType

delete

deleteAll

deleteAllAttachments

deleteAttachment

equals

errorFileName

errorMessage

exists

fileDescriptor

fileName

fixImageDimensions

get

getAllAttachments

getAsStream

getAttachmentRecord

getAttachments

getAttachmentsForExtensions

getBytes

getBytesUnsafe

getClass

getContentStream

getContentType

getDBImageAttachmentRecord

getErrorFileName

getErrorMessage

getFileDescriptor

getFileName

getInputStream

getParameter

getParameters

getSizeBytes

getSysID

getTableName

getTableSysID

hashCode

imageDimensions

inputStream

loadParameters

notify

notifyAll

parameter

parameters

processRequest

read

readByName

readToFile

renameAttachment

replaceContent

setContentType

setFileName

setImageDimensions

setInputStream

setStreamCipher

setStreamMonitor

setTableName

setTableSysID

sizeBytes

streamCipher

streamData

streamMonitor

sysID

tableName

tableSysID

toString

wait

write

writeContentStream

writeFromRequest

writeInvisible

writeParts

7 REPLIES 7

Hello Peter,



Were you able to figure that out or an alternate method to get the base64 encoding?



Thanks,


Probir


This is what we do now



var attachmentIS = new GlideSysAttachmentInputStream(attachment.getValue('sys_id'));


  var bytearrayOS = new Packages.java.io.ByteArrayOutputStream();


  attachmentIS.writeTo(bytearrayOS);


  var content64 = GlideBase64.encode(bytearrayOS.toByteArray());



Though we have had JavaException with large attachments as the content64 was not large enough to contain the content of the file. As it was a JavaException and not a Javascript exception, it was also impossible to catch and log. So it's a good workaround for now, but we did not find time to find a better solution.


Thanks for the code snippet peterraeves, that's put me on the right track!

Turns out you can use GlideBase64.encode() on a GlideSysAttachment byte array / stream / whatever it is in the background.

var gsa = new GlideSysAttachment();
var attachmentContentBase64 = GlideBase64.encode(gsa.getBytes(grAttachment));