Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to copy attachments from RITM to taks

RudhraKAM
Tera Guru

Hello

i would like to copy when the attachment is attached to RITM it should copy over to associated Task as well , i searched in the community , some have i found that business rule , but the issue i am facing here is 

When i mark the business rule to run on insert , its not performing any action  meaning if i attach an image and if i open the associated task cannot see the image.

and when i change the business rule from insert to update , its acting even strange , when i attach any attachment on RITM and save it and open the task , i cannot see any attachment and if i save the task then i can see the attachement and  if i save it again its duplicating the attachement . Am i missing any thing here , can some one help me with this please,

find_real_file.png

 

find_real_file.png

 

1 ACCEPTED SOLUTION

Let's try below code on sys_attachment table with after insert BR, I have tested and it works fine. 

(function executeRule(current, previous /*null when async*/) {
	var sctask = new GlideRecord('sc_task');
	sctask.addQuery('request_item', current.table_sys_id);
	sctask.addQuery('request_item.cat_item', 'PASS THE SYS_ID OF CATALOG ITEM HERE');
	sctask.query();
	if(sctask.next()){
		var deleteTaskAttachment = new GlideRecord('sys_attachment');
		deleteTaskAttachment.addQuery('table_sys_id', sctask.sys_id.toString());
		deleteTaskAttachment.addQuery('table_name', 'sc_task');
		deleteTaskAttachment.deleteMultiple();
		
		var ritmAttachment = new GlideRecord('sys_attachment');
		ritmAttachment.addQuery('table_sys_id', sctask.request_item);
		ritmAttachment.query();
		if(ritmAttachment.next())
			GlideSysAttachment.copy('sc_req_item', ritmAttachment.table_sys_id, 'sc_task', sctask.sys_id); 
	}
})(current, previous);

View solution in original post

22 REPLIES 22

it is working ,but if i attach a file A in RITM  and open the task it is showing file A 

and if i attach a file B in RITM, In task it is showing File A , File A, File B 

I am just using the Insert operation for the business rule

Can you change that to before insert and try. else try below code if this helps.

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.table_sys_id);
gr.addQuery('request_item.cat_item', 'PASS THE SYS_ID OF CATALOG ITEM HERE');
gr.query();
if(gr.next()){
var gr1 = new GlideRecord('sys_attachment');
gr1.addQuery('table_sys_id', gr.request_item);
gr1.orderByDesc('sys_created_on');
gr1.setLimit(1);
gr1.query();
if(gr1.next())
GlideSysAttachment.copy('sc_req_item', gr1.table_sys_id, 'sc_task', gr.sys_id);
}
})(current, previous);

were you able to make it work?

for both solutions its acting as same 

 

if i attach a file A in RITM  and open the task it is showing none

and if i attach a file B in RITM, In task it is showing File A 

if i attach file c then its showing file A , File A, File B 

Let's try below code on sys_attachment table with after insert BR, I have tested and it works fine. 

(function executeRule(current, previous /*null when async*/) {
	var sctask = new GlideRecord('sc_task');
	sctask.addQuery('request_item', current.table_sys_id);
	sctask.addQuery('request_item.cat_item', 'PASS THE SYS_ID OF CATALOG ITEM HERE');
	sctask.query();
	if(sctask.next()){
		var deleteTaskAttachment = new GlideRecord('sys_attachment');
		deleteTaskAttachment.addQuery('table_sys_id', sctask.sys_id.toString());
		deleteTaskAttachment.addQuery('table_name', 'sc_task');
		deleteTaskAttachment.deleteMultiple();
		
		var ritmAttachment = new GlideRecord('sys_attachment');
		ritmAttachment.addQuery('table_sys_id', sctask.request_item);
		ritmAttachment.query();
		if(ritmAttachment.next())
			GlideSysAttachment.copy('sc_req_item', ritmAttachment.table_sys_id, 'sc_task', sctask.sys_id); 
	}
})(current, previous);