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

Arjun Kumar Le1
Tera Contributor

below is the script, please ignore the above posted script.

 

var gr=new GlideRecord('incident');

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

Aniket Chavan
Tera Sage
Tera Sage

Hello @Arjun Kumar Le1 ,

Please give a try to the script below and let me know your views on this.

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

if (incidentGr.next()) {
    GlideSysAttachment.copy("incident_task", current.sys_id, "incident", incidentGr.sys_id);
}

// 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();
    }
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

It is not working in Removal(deletion), it is only working if copy the attach ment  only for first time , while second time copy any document in incident _task, it is not copying on incident.

 

Please help, if any change is required in it.

Hello @Arjun Kumar Le1 ,

 

Okay let's then let's try with the below updated response and let me know how it works for you.

// 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();
    }
}