Copy Attachment from ritm to task and vice versa without looping

Rahul Gupta
Tera Contributor

I have to copy the attchment from ritm to task and also from task to ritm ....but it goes in the loop when do it using the business rule ....pls help 

how can i break the loop in the business rule ? 

9 REPLIES 9

Sumanth16
Kilo Patron

In my requirement i dont want to do it using the related list 

 

AbhishekGardade
Giga Sage

Hello Rahul,

Can you share your code so we debug?

Thank You!

Abhishek Gardade

ServiceNow MVP 2020

Thank you,
Abhishek Gardade

Yeah sure @Abhishek Gardade 

from ritm to task 

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

var reqtask = new GlideRecord('sc_task');
reqtask.addQuery('request_item', current.table_sys_id);
reqtask.addQuery('table_name', 'sc_req_item');
reqtask.query();
while (reqtask.next()) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', reqtask.sys_id);
att.query();
while (att.next()) {
att.deleteRecord();
}
GlideSysAttachment.copy('sc_req_item', current.table_sys_id, 'sc_task', reqtask.sys_id);
}
})(current, previous);

from task to ritm


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

var reqItem = new GlideRecord('sc_task');
if (reqItem.get(current.table_sys_id)) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', reqItem.request_item.sys_id);
att.query();
while (att.next()) {
att.deleteRecord();
}
GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', reqItem.request_item.sys_id);
}

})(current, previous);

 

Till here it is working fine just helpme how can i break the loop