Create .txt file in sys_attachment from data stored in a table

jogmen
Giga Contributor

Has anyone programatically created a text file attachment from data stored in a field in a ServiceNow table from a business rule?

I am essentially trying to emulate the behavior in the Attachment Note workflow activity. I need to create an attachment, but on a table that doesn't support that activity - so I want to include a Run Script activity with code that will allow me to create an attachment on the record.

I was thinking something like:

var file = new GlideRecord("sys_attachment");
file.file_name = "testname.txt";
file.CONTENTS = VARIABLECONTENT;
var sysID = rdp_file.insert();

But am not sure how to actually funnel the text from my variable into the content for the attachment record.

Any help would be appreciated.

9 REPLIES 9

Nice - so I'd guess you'd be able to create a custom Excel too using this method (albeit, with a more complicated payload)?


Cool!


Hi Jogmen,

 

Gettring below error, when I use var StringUtil = Packages.com.glide.util.StringUtil;
var encodedPayload = StringUtil.base64Encode(attachmentPayload); lines inside function as well.

Any idea?

Error mid server : JavaException: java.lang.SecurityException: Illegal attempt to access class 'com.glide.util' via script

Thanks!

Hello Amutha,

 

Calls to package are more and more restrained by ServiceNow.

 

But for this case, there is an alternative, you can use :

                var stringUtil = new GlideStringUtil(),
                    encodedPayload = stringUtil.base64Encode(attachmentPayload);

This should work in current versions.

Vishnu V Reddy
Tera Contributor

IS there is a way to attach body from inbound email into an incident as an "attachment", every time an incident is created?