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

I am not sure about this.. I can post the screenshoots of the related list.


sachin312
Giga Expert

Can post a screenshot of a "case" and the related lists.


sn.PNG


this is the screen shot of the related list of the case.


Just try the below script. Let me know if it worked for you or not.


sachin312
Giga Expert

The below script will copy attachments inserted on 'incident' to the problem given in the problem field on incident form.



Business rule after insert on 'sys_attachment':



function onAfter(current, previous) {


    //This function will be automatically called when this rule is processed.


  gs.log('hello');


  var incGr = new GlideRecord('incident');


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


                incGr.query();


  while(incGr.next()){


  gs.log(incGr.sys_id);


          var gr = new GlideRecord('problem');


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


    gr.query();


  while(gr.next()){


            GlideSysAttachment.copy('incident', current.table_sys_id, 'problem', gr.sys_id);



  var attach = new GlideRecord('sys_attachment');


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


        attach.query();


        while(attach.next()){


      if(attach.file_name != current.file_name){


          attach.deleteRecord();;


                  }


                    }



  }


  }



}




Let me know if it works for you or not.