copy attachments to case to incident and vice-versa

srikanth1992
Kilo Contributor

Hello Community,

My requirement is to copy attachments from case to the incident i.e.,(when ever we added an attachment to the case that should also be seen in the related incident ) and also Vice-versa the opposite way. I tried with writing a business rule (for copying attachments to case-incident) on Sys_attachment table.

After - Insert

script :

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

 

  // Add your code here  

 

  var incGr = new GlideRecord('incident');  

  incGr.addQuery('case', current.sys_id);  

  incGr.query();

  while(incGr.next())  

  {  

  var gr = new GlideRecord('sys_attachment');

  gr.addQuery('table_sys_id', incGr.sys_id);

  gr.deleteMultiple();

  GlideSysAttachment.copy('sn_customerservice_case', current.table_sys_id, 'incident', incGr.sys_id);  

  }  

 

})(current, previous);  

This didnt work for me and also this is landing no where(I am unable to attach or close after activating the BR.). Any help will be appreciated.

11 REPLIES 11

Akshat Chawla
Giga Expert

Hi,



Try below code :



Condition : current.table_name == '<table of which you want to copy the attachment>'



var incGr = new GlideRecord('incident');


incGr.addQuery(<'field name which in on incident and refer the table(case) of which you want to copy the attachment'>, current.table_sys_id);


incGr.query();


if(incGr.next())


{


  GlideSysAttachment.copy('<soruce tabele name>', current.table_sys_id, 'incident', incGr.sys_id);


}



table-Attachment(sys_attachment)


after-insert




condition : Condition : current.table_name == 'case'



script:



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




  // Add your code here


  var incGr = new GlideRecord('incident');    


incGr.addQuery('parent',current.table_sys_id);  


incGr.query();    


if(incGr.next())    


{    


  GlideSysAttachment.copy('case', current.table_sys_id, 'incident', incGr.sys_id);    


}    




})(current, previous);


This is the way I did.This didn't worked . Can you suggest me any modifications.


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Instead of copying the attachments, why not create a Defined Related List to expose the related attachments?   This way you aren't duplicating the attachment in the event that it gets updated on one record.   More about creating defined related lists can be found here:


http://wiki.servicenow.com/index.php?title=Creating_Defined_Related_Lists#gsc.tab=0


sachin312
Giga Expert

Hi Srikanth,



Do the case table have any field that reference to the related incident?