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.

Copy the attachment again if already exist in the target table

Arjun Kumar Le1
Tera Contributor

Hi every one,
I am copying attachment from work order task to work order but if already exist attach emnt in target record, it copying , i want to avoid it, please help me how to do it.
below is the script i have written after inser and update business rule on work order task table, please help in it.

if (current.operation() !== 'insert' && current.operation() !== 'update') {
        return;
    }

    var incidentGr = new GlideRecord('incident');
    incidentGr.get(current.getValue('incident'));

    if (incidentGr.isValidRecord()) {
        // Check if there are attachments to copy
        GlideSysAttachment.copy('incident_task', current.sys_id, 'incident', incidentGr.sys_id);
    }

 

 

4 REPLIES 4

Arjun Kumar Le1
Tera Contributor
 var gr = new GlideRecord('wm_order');
    gr.addQuery('sys_id', current.parent);
    gr.query();
    while (gr.next()) {
        //  var attach = new GlideSysAttachment();
        //      //attach.deleteAll(gr);
        GlideSysAttachment.copy("wm_task", current.sys_id, "wm_order", gr.sys_id);
    }

the above is the script, please correct the above

 

var gr = new GlideRecord('wm_order');
gr.addQuery('sys_id', current.parent);
gr.query();

while (gr.next()) {
// Check if the attachment already exists on the destination record
if (!attachmentExists("wm_order", gr.sys_id, current.sys_id)) {
// If it doesn't exist, then copy the attachment
GlideSysAttachment.copy("wm_task", current.sys_id, "wm_order", gr.sys_id);
}
}

/**
* Check if an attachment already exists on the destination record.
*
* @Param {string} destTable - The destination table name.
* @Param {string} destRecordSysId - The sys_id of the destination record.
* @Param {string} sourceSysId - The sys_id of the source record.
* @returns {boolean} - True if the attachment already exists, false otherwise.
*/
function attachmentExists(destTable, destRecordSysId, sourceSysId) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', destTable);
attachment.addQuery('table_sys_id', destRecordSysId);
attachment.addQuery('file_name', current.name); // Assuming 'name' is the attachment file name field
attachment.query();

return attachment.hasNext();
}

VaishnaviShinde
Kilo Sage

Hello @Arjun Kumar Le1 ,

 

Created on insert BR for sys_attachment table. Add below script.

 

if (current.table_name == 'incident') { // Add your table Name
        var incident = new GlideRecord('incident'); // Add your table Name
        incident.addQuery('sys_id', current.table_sys_id);
        incident.query();
        if (incident.next()) {
            var problem = new GlideRecord('problem'); // Add your child table Name
            problem.addQuery('first_reported_by_task', current.table_sys_id);
            problem.query();
            while (problem.next()) {
                var attachment = new GlideRecord('sys_attachment');
                attachment.addQuery('table_sys_id', problem.getUniqueValue());
                attachment.query();
                if (!attachment.hasNext()) {
                    gs.info('In if');
                    GlideSysAttachment.copy('incident', incident.sys_id, "problem", problem.sys_id); // Change table names
                }
                problem.update();
            }
        }

    }

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Vaishnavi Shinde