Attachment files other than "txt" type are created as corrupted files

Hitesh Patel3
Tera Contributor
var decodedContent = GlideStringUtil.base64Decode(content);
var gsa = new GlideSysAttachment();
var attachmentSysID = gsa.write(grTarget, fileName, contentType, decodedContent);

Where -

  • "content" is base64 encoded text for the attachment file.
  • "grTarget" is the object of the target record.
  • "filename" is the attachment file name.
  • "contentType" is the type of the attachment file to be created.

           
The above code is working properly when the contentType is "txt" but for other types of files (images/pdf etc.) it's not working properly - it's creating corrupted image/pdf. Any idea - what's the issue here?
I tried using writeBase64() function instead of just write() - but no help!

P.S: I tried to paste the raw base 64 encoded text into https://www.base64decode.org/ and decoded it from there manually - it created the image/pdf files in expected format.

Message was edited by: Hitesh Patel

1 ACCEPTED SOLUTION

amlesh
Kilo Sage

Please use 'base64DecodeAsBytes'


View solution in original post

4 REPLIES 4

amlesh
Kilo Sage

Please use 'base64DecodeAsBytes'


Thanks Amlesh for the answer. Using the function base64DecodeAsBytes() instead of base64Decode() resolved my issue.


But GlideStringUtil is not allowed in scoped applications. Then what would be the solution for Scopped apps?

you can create a global script include with the attachment parse code and call it from your scope.