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 all attachments from child to parent record when child record is completed && delete the duplicate attachments from parent record when copying the attachments

Vijaya13
Tera Contributor

I have done the work on coying all the attachments from parent to child record when child record is created and it works fine.

Now, I need to copy all attachments from child to parent record when child record is completed && delete the duplicate attachments from parent record when copying the attachments from child record. How can I do this?

We can take the example of incident and incident task

 

 

9 REPLIES 9

Mahendra RC
Mega Sage

Hello,

Please check with below script:

var parentRecord = new GlideRecord('x_pgpl2_spend_pre_contract');
    parentRecord.addQuery('sys_id', current.sys_id);
    parentRecord.query();
    if (parentRecord.next()) {
        var glideAttachment = new GlideSysAttachment();
        glideAttachment.deleteAll(parentRecord);
        glideAttachment.copy('x_pgpl2_spend_pre_contract_task', current.getValue("sys_id"), 'x_pgpl2_spend_pre_contract', parentRecord.getValue("sys_id"));
    }

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

Didn't work. It was not copying any attachments to parent record.

Hello Vijaya,

My apologise I think I pasted incorrect code. Could you please check once with below code:

var parentRecord = new GlideRecord('x_pgpl2_spend_pre_contract');
    parentRecord.addQuery('parent', current.getUniqueValue());
    parentRecord.query();
    if (parentRecord.next()) {
        var glideAttachment = new GlideSysAttachment();
        glideAttachment.deleteAll(parentRecord);
        glideAttachment.copy('x_pgpl2_spend_pre_contract_task', current.getValue("sys_id"), 'x_pgpl2_spend_pre_contract', parentRecord.getValue("sys_id"));
    }

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

tried with this but didn't worked.

I found the solution with below script:

(function executeRule(current, previous /*null when async*/ ) {
    var parentRecord = new GlideRecord('x_pgpl2_spend_pre_contract');
    parentRecord.addQuery('number', current.parent.number);
    parentRecord.query();
    //     var rowCount = parentRecord.getRowCount();
    //     gs.info('RowCount is: ' + rowCount);
    if (parentRecord.next()) {
        var at = new GlideRecord('sys_attachment');
        at.addQuery('table_sys_id', current.parent.sys_id);
        at.query();
        if (at.next()) {
            at.deleteMultiple();
        }
        GlideSysAttachment.copy('x_pgpl2_spend_pre_contract_task', current.sys_id, 'x_pgpl2_spend_pre_contract', parentRecord.sys_id);
    }
})(current, previous);