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.

Alternative of attachment functions in global scope

Amanjyot Oberoi
Tera Contributor

May i know what is the alternative of getcontent , getcontentbase64, writecontentbase64 in global scope.

These functions of glidesysattachment class work fine in scoped app ony. Wondering if we have similar functions for global scope. I need to read/write attachments using scripts.

 

Thanks in advance!

1 REPLY 1

Oleg
Mega Sage

You can use new GlideSysAttachment().getBytes method to get the content from GlideRecord of "sys_attachment" table. You can convert the bytes to Base64 using GlideStringUtil.base64Encode. If the attachment is a text file then you can use Packages.java.lang.String to convert bytes to string.

For example, I added to an incident with sys_id="e128e85adb4d230095a810825b961960" HTML file test.html. I can use the following code to read the attachment:

var attachmentGR = new GlideRecord("sys_attachment");
attachmentGR.addQuery("file_name", "test.html");
attachmentGR.addQuery("table_name", "incident");
attachmentGR.addQuery("table_sys_id", "e128e85adb4d230095a810825b961960");
attachmentGR.query();
if (attachmentGR.next()) {
    var bytesContent = new GlideSysAttachment().getBytes(attachmentGR);
    var base64ImageStr = GlideStringUtil.base64Encode(bytesContent);
    gs.print("base64ImageStr=" + base64ImageStr);
    var strData = Packages.java.lang.String(bytesContent);
    gs.print("strData=" + strData);
}

P.S. See Script Includes GeneralFormAPI and SoapAttachments for code examples.