Facing issue in copy attachment from incident task to incident

Arjun Kumar Le1
Tera Contributor

Hi Team,
I have written a script and using it in after insert and update Business rule, i am able to copy the attachments but.

1.the attachment only copying one time, and the second time it is not copying.

2.how to to delete the attachment in Incident, when delete the attachment in  Incident_task  table.

 

var gr=new GlideRecord('incident');
gr.addQuery('sys_id',current.incident);
gr.query();
gs.addInfoMessage(gr.getRowCount());
while(gr.next())
 {
var attach = new GlideSysAttachment();
attach.deleteAll(gr);
GlideSysAttachment.copy("incident_task",current.sys_id,"incident",gr.sys_id);
}
gr.update();

7 REPLIES 7

no change , still same

 

Hello @Arjun Kumar Le1 ,

 

Let's try a different approach. Instead of relying on the business rule, we can use an event-driven script. Please create an after business rule on the incident_task table for insert and update operations, and use the following script:

// Copy attachments from incident_task to incident
var incidentGr = new GlideRecord('incident');
incidentGr.addQuery('sys_id', current.incident);
incidentGr.query();

if (incidentGr.next()) {
    // Check if there are attachments to copy
    var attachmentCount = GlideSysAttachment.copy("incident_task", current.sys_id, "incident", incidentGr.sys_id);
    
    if (attachmentCount > 0) {
        gs.addInfoMessage("Attachments copied successfully.");
    }
}

// Delete attachments in incident when deleted in incident_task
if (current.operation() == 'delete') {
    var delAttachments = new GlideRecord('sys_attachment');
    delAttachments.addQuery('table_name', 'incident');  // Replace 'table_name' with the correct field name
    delAttachments.addQuery('table_sys_id', current.incident);  // Replace 'table_sys_id' with the correct field name
    delAttachments.query();

    while (delAttachments.next()) {
        delAttachments.deleteRecord();
    }
}

same no change, same , can we set up a call to see the issue