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
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

Pradeep Sharma
ServiceNow Employee
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


But can I select which attachments to copy? GlideSysAttachment.copy seems to copy all attachments over it says.


Yes you can select which attachment to be copied. Pass source table and sys_id parameters i.e sys_attachment table and sys_id of the attachment record.


Please let me know if you have any questions.


I believe GlideSysAttachment.copy will not work when you are trying to copy from sys_attachment table directly. Instead you will have to query on the attachment table and find the attachment record you want to insert on all the requests and insert a new attachment in the sys_attachment table using a before business rule.


Note: The downside of it is you will end up creating a duplicate each time a request is inserted



Create a new business rule on sc_request table


when: before insert


Script:


var attachmentGR= new GlideRecord('sys_attachment');


  attachmentGR.addQuery('file_name','<your file name goes here>'); //put your file name here


  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_bites = attachmentGR.size_bites;


  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();


}