copy attachments from request to RITM

kunal16
Tera Expert

Hi,

I have a requirement to copy all the attachments from Request to the corresponding RITMs.

I have written an After-Insert Business rule on the Request table to achieve the same. But it seems the code is not working as expected.

Name: Copy Attachments to RITM

Table: Request (sc_request)

When to run: After - Insert

Script:

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

  // Add your code here

  var ritm = new GlideRecord('sc_req_item');

  ritm.addQuery('request', current.sys_id);

  ritm.query();

  while(ritm.next())

  {

  GlideSysAttachment.copy('sc_request', current.sys_id, 'sc_req_item', ritm.sys_id);

  }

})(current, previous);

Any lead will be appreciated. Thanks in advance!!

22 REPLIES 22

Suresh1
Tera Guru

Hi Kunal,



Try out the below community link:



How to copy RITM attachments to Requests



Thanks,


Suresh


Kalaiarasan Pus
Giga Sage

I wouldn't copy the attachments as these would just add duplicate records. Instead try a custom related list.



"Related Attachments" Related List


Improving the Attachments List View


Showing Requested Item Attachments on the Catalog Task Form  


Hi Kalai,


Thanks for your reply. But actually I want to copy attachments so that users will be able to see the same attachments on the Service portal view. The community thread will be fine for the natiave view but for my requirement, I need to copy attachments from request to RITM


tanumoy
Tera Guru

Writing an After-Insert Business rule on the Request table will not work here. Because when you are attaching an attachment in a REQ, it is not inserting anything on Request table.



Write the below code in a After-Insert BR on sys_attachment table:



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



  var ritm = new GlideRecord('sc_req_item');  


  ritm.addQuery('request', current.table_sys_id);  


  ritm.query();  


  while(ritm.next())  


{


  GlideSysAttachment.copy('sc_request', current.table_sys_id, 'sc_req_item', ritm.sys_id);


}



})(current, previous);



But, it will create duplicate records. Below link can help to remeove duplicates. Or you have one other way also. You have to delete all the attachment first then have to copy.



Copy Attachment with out duplicates