How to copy attachments from incident to problem

Shiva prasad t
Tera Guru

When we are trying to create problem from incident, the attachments are not copying / getting duplicate vice versa.

When: on Update

Condition: Problem is not Empty

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

 

var gr = new GlideRecord('problem');

               gr.addQuery('sys_id',current.problem);

               gr.query();

               if(gr.next())

                              {

                                             gr.problem = current.sys_id;

                                             gr.update();

                              }

              

                                             var temp = GlideSysAttachment.copy('incident',current.sys_id,'problem',current.problem);

                                            

                             

 

})(current, previous);

 

6 REPLIES 6

Raghu Ram Y
Kilo Sage

Hi,

Try like below.

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

	var gr=new GlideRecord('problem');
	gr.addQuery('sys_id',current.problem);
	gr.query();
	if(gr.next())
	{	
	GlideSysAttachment.copy("incident",current.sys_id,"problem",gr.sys_id);
	}
	gr.update();
})(current, previous);

Community Alums
Not applicable

Hi @fred ,

here is the script which you might want to Refer/use:

Business rule: Copy Incident attachment into Problem

after

table : sys_attachment

insert, update checked

Script:

var atta = new GlideRecord('problem');

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

  atta.query();

  if(atta.next())

{

  var gr = new GlideRecord('sys_attachment');

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

  gr.deleteMultiple();

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

current.update();

}

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

ersureshbe
Giga Sage
Giga Sage

Hi,

I would recommend to use below article. As per Service now best practice dont copy the attachment instead you can display the attachment under related attachment tab

. https://community.servicenow.com/community?id=community_blog&sys_id=928c2ae1dbd0dbc01dcaf3231f961927

Regards,

Suresh.

Regards,
Suresh.

shloke04
Kilo Patron

Hi @fred 

I learnt recently from one of the expert that copying attachment is not a recommended approach and it makes sense as well as it duplicate the attachment on multiple tables which from Architectural point of view is not a recommended approach.

Suggested approach would be using the related list as below:

https://community.servicenow.com/community?id=community_blog&sys_id=928c2ae1dbd0dbc01dcaf3231f961927

However if still you need to go down the path and you need Attachment then I would suggest use Flow Designer approach as using Glide Attachment Copy will duplicate the attachment as by default it copies all the attachment every time the API is called and leads to duplication.

Navigate to Flow Designer and click on New and then Select Flow.

Then fill out the form and make sure to select it as Run as System User as shown below:

find_real_file.png

find_real_file.png

Now build your flow as shown below:

Final Flow will look like below:

find_real_file.png

 

Sharing Individual screenshot of the flow as well:

Trigger Condition:

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

This will not duplicate the attachment when it is updated. This works as well in my PDI have tested it .

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke