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

syedfarhan
Kilo Sage

HI ,



Try this :



v


(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.sys_id); //current.sys_id


  }


  }


  gs.info("completed");



})(current, previous);



Thanks


will this work? Current.sys_id will give me the sys id of the email table record. But I want the sys_id of the HR Case record


First, find out where the source record really is. You believe it has a record in sys_attachment... what table is that attachment attached to?


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