How can I create an attachment from script?

Joey Wan Kenobi
Tera Guru

Does SN offer any API to create an attachment?  I've used the following:

 

var gsa = new GlideSysAttachment(); var attachmentId = sa.write(gr, "fileName.txt", 'text/plain', "some data");

 

This works fine for creating text files. It doesn't work when trying to create a pdf, doc or docx file. Is there a way to do so?

22 REPLIES 22

Hi gajji,

try using the below code snippet ,I hope this will help you.

     var attach=new Attachment();

attach.write(tablename,rec sysid,filename,content type,value) ;

 

//here tablename is the table were you need to attach the attachment.

// rec sysID for the record on which you need to attach the attachment.

// File name is the name of attachment file with extentension like "filename.pdf".

//content type is the extenstion for file in your case it is "application/pdf".

//value is the base64 of the attachment file .

 

If Found helpful please mark as helpful.

   

vg47641
Tera Contributor

PDF Format

 

var gsa = new GlideSysAttachment();

var attachmentId = gsa.write(gr, "fileName.txt", 'application/pdf', "some data");

 

 

 

 

if it's correct above solution click helpful

Shehzad Mansur1
Tera Contributor

var gsa = new GlideSysAttachment();

You just need to add document type as the third parameter correctly. If it is docx then "application/docx" and if PDF then "application/pdf".

var cont_type = (documentType== "docx") ? "application/docx" : "application/pdf";

var attachmentId = sa.write(gr, "fileName.txt",cont_type , "some data");