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

srikanth1992
Kilo Contributor

Wrote an Afetr-Insert business rule :


condition : current.table_name=='sn_customerservice_case' && current.created_by!='attachcopy'


Script:


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




  // Add your code here


  var cas = new GlideRecord('sn_customerservice_case');  


cas.addQuery('sys_id', current.table_sys_id);  




cas.query();  


if(cas.next()){  


  var attach = new GlideRecord('sys_attachment');  


  attach.initialize();  


  attach.file_name = current.file_name;  


  attach.content_type = current.content_type;  


  attach.compressed = current.compressed;  


  attach.table_name = 'incident';  


  attach.size_bytes = current.size_bytes;  


  attach.size_compressed = current.size_compressed;


  attach.created_by ='attachcopy';


  var inc = new GlideRecord('incident');


  inc.addQuery('parent', cas.sys_id);  


  inc.query();


  if(inc.next()){


  attach.table_sys_id = inc.sys_id;


  }


   


  var aId = attach.insert();  


 


  var aDoc = new GlideRecord('sys_attachment_doc');  


  aDoc.addQuery('sys_attachment', current.sys_id);  


  aDoc.query();  


  while(aDoc.next()){  


  var newDoc = new GlideRecord('sys_attachment_doc');  


  newDoc.initialize();  


  newDoc.sys_attachment = aId;  


  newDoc.position = aDoc.position;  


  newDoc.length = aDoc.length;  


  newDoc.data = aDoc.data;  


  newDoc.insert();  


  }  


}  




})(current, previous);



This script worked for me.


Thanks


Vattem Sandhya
Tera Contributor

Hi 

In our case attachments will copy from case to incident or change but it will copy after update any attachments on case.

what I need is if case is attached to any attachment initially that will copy to incident/change.

How can I make this work?

Thank you in advance for your help.