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

p_espinar
Kilo Guru

Hello Rahul,

in order to avoid loops, you have to use the sentence glideRecord.setWorkflow(false) to not to run business rules in your update.

Un saludo,
Pablo Espinar
Consultant at Econocom Spain

Please mark this response correct if I've answered your question. Thanks!

 

 

above i posted my code in the comment how should i use this in my code ?

Rahul Gupta
Tera Contributor

I got the Solution to break the loop

 

Copy RITM Attachments to task

 

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

var flag = 0;
var reqtask1 = new GlideRecord('sc_task');
reqtask1.addQuery('request_item', current.table_sys_id);
reqtask1.query();
while (reqtask1.next()) {

var canAttach = new GlideRecord('sys_attachment');
canAttach.addEncodedQuery('table_sys_id=' + reqtask1.sys_id + '^sys_updated_onBETWEENjavascript:gs.beginningOfLastMinute()@javascript:gs.endOfCurrentMinute()');
canAttach.query();
if (canAttach.next()) {
flag = 1;
}
}

if (flag == 0) {
var reqtask = new GlideRecord('sc_task');
reqtask.addQuery('request_item', current.table_sys_id);
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);

 

 

Copy Task Attachments to RITM

 

 

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

var flag = 0;
var reqItem1 = new GlideRecord('sc_task');
if (reqItem1.get(current.table_sys_id)) {

var canAttach = new GlideRecord('sys_attachment');
canAttach.addEncodedQuery('table_sys_id=' + reqItem.request_item + '^sys_updated_onBETWEENjavascript:gs.beginningOfLastMinute()@javascript:gs.endOfCurrentMinute()');
canAttach.query();
if (canAttach.next()) {
flag = 1;
}
}

if (flag == 0) {

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);
//att.setWorkflow(false);
}
}
})(current, previous);

 

 

On which form we should add above business rule ?

mohamadalha
Tera Contributor

You can create a Flow to handle this, which will prevent loops and duplicate attachments