Copy attachment from one table to another

sidarth
Giga Expert

Hi All,

I have written a Business rule to copy attachment from sys_attachment to hr_case table when someone sends and attachment via the email client template on a hr record.

(function executeRule(current, previous /*null when async*/) {

  // Add your code here

var sys_id=current.sys_id;

  if(current.target_table=="hr_case"){

  var gr= new GlideRecord('sys_attachment');

  gr.addQuery("table_sys_id",sys_id);

  gr.query();

  if(gr.next()){

  GlideSysAttachment.copy('sys_attachment', gr.sys_id, current.target_table, current.instance);

  }

  }

  gs.info("completed");

})(current, previous);

But it is not working. I cannot see any attachment on HR Case table.

Also in the logs I see the following warning : "syslog_transaction not found for AjaxClientTiming: sysId: , table: , view: Default view, form: email_client_template"

Any help would be appreciated

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Sidarth,



The key to making this work is to find out what the real record is that the attachment is linked to. The attachment is IN sys_attachment, but it is not attached to a sys_attachment record.



If you look in sys_attachment for that record, you will see the table name (see example image below.)



find_real_file.png



THAT is going to be your source table and sys_id to use in the first arguments. The target table will be the current record's information (see 3rd and 4th args below.)



GlideSysAttachment.copy(sourceTable, sourceSysId, current.getTableName(), current.sys_id);


View solution in original post

7 REPLIES 7

Thanks for the clarification.


Gaurav Kashyap
Mega Expert

Hi Sidarth,



You won't be able to copy from sys_attachment table. From sys_attachment find out on which record of which table the attachment is attached to and copy it from there to your hr_case table.



Regards,


Gaurav


Sairam3697
Tera Contributor

Did you resolve this ?