Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Auto-attaching attachment to a record

lightblu
Mega Expert

Hi,

I would like an attachment (word document) to be attached to each new record (in my request table) by default, as soon as the record gets created. How would I do this?

Right now I have to drag-and-drop the attachment onto each record.

Thanks!

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee

Hi,



You can upload attachment in sys_attachment table and then create a before business rule on incident table to copy attachment from sys_attachment table to incident table with the help of GlideSysAttachment.copy


More info here Copy Attachments from Record to Record - ServiceNow Wiki


View solution in original post

13 REPLIES 13

Hi All,



I had a requirement to get this done on a catalog item based on the selection in one of the variables.   That being the case I did this in the workflow, but the same logic should work in a BR as well if you want to add one or more attachments when a record gets inserted into sc_request or any other table.



Cameron was on the right track about inserting into sys_attachment_doc, but depending on the size of the attachment, you may have to insert multiple records.   This is where the actual attachment data is stored.



Note:   size_bites as seen in Abhinay's script above has changed to size_bytes



if (current.variables.request_type == 'transfer_phone') {



/* I attached the needed attachment to a record in the rm_enhancement table.   You can store it anywhere, but it's important to use the sys_id from sys_attachment to grab it.   If you were to store it in sc_request or sc_req_item, there will be multiple with the same name so you don't want to query using the file name.   You'll see the importance of this when you get to sys_attachment_doc   */



var attachmentGR= new GlideRecord('sys_attachment');


attachmentGR.addQuery('sys_id','359651a34f728300c09001b28110c7f2');


attachmentGR.query();



if(attachmentGR.next()) {


var newGR = new GlideRecord('sys_attachment');


newGR.initialize();


newGR.compressed = attachmentGR.compressed;


newGR.content_type = attachmentGR.content_type;


newGR.size_bytes = attachmentGR.size_bytes;


newGR.size_compressed = attachmentGR.size_compressed;


newGR.file_name = attachmentGR.file_name;


newGR.table_name = current.getTableName();


newGR.table_sys_id = current.getValue('sys_id');


var newID = newGR.insert();



var Doc = new GlideRecord('sys_attachment_doc');


Doc.addQuery('sys_attachment', '359651a34f728300c09001b28110c7f2');   // note the same sys_id as used above. could have used attachmentGR.sys_id


Doc.query();



var newDoc = new GlideRecord('sys_attachment_doc');


while (Doc.next()) {


newDoc.initialize();


newDoc.sys_attachment = newID;


newDoc.length = Doc.length;


newDoc.data = Doc.data;


newDoc.position = Doc.position;


newDoc.insert();


}


}


}



Please mark helpful or correct if you find it so...


Jon


Hi Abhinay, 

I had the similar requirement but the table is HR case table instead of Request Table. I used your script and I am able to add the attachment to the Hr case but the attachment does not open. It gives error message "Failed to load PDF Document".

Any idea why I am unable to open the file. If i add the same attachment manually, it gets open.

Can you please suggest?

 

Thanks in advance.

 

Think about sys_attachment as a pointer.  It contains the name of the attachment and the attachment will show up on the ticket, but without getting the data from sys_attachment_doc there's nothing in the attachment.  This is why you're seeing errors when trying to open it

I was able to achieve this by querying sys_attachment_doc. the script mentioned above helped me.

Thanks.