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

Mohith Devatte
Tera Sage
Tera Sage

HELLO ,

YOU CAN DO THIS 

var gr = new GlideRecord('your_parent_table');
gr.addQuery('your_query');
gr.query();
if(gr.next())
{
var attach = new GlideSysAttachment();
attach.deleteAll(gr);
attach.copy('child_table',child_table_record_sys_id, 'parent_table',gr.sys_id);
}

Hope this helps 

Mark the answer correct if this helps you

Im confused with first query here?

 

    var gr = new GlideRecord('x_pgpl2_spend_pre_contract');
    gr.addQuery('sys_id', current.sys_id);
    gr.query();
    if (gr.next()) {
        var attach = new GlideSysAttachment();
        attach.deleteAll(gr);
        attach.copy('x_pgpl2_spend_pre_contract_task', current.sys_id, 'x_pgpl2_spend_pre_contract', gr.sys_id);
    }

 

 

hey 

No so you glide recorded the current parent table record and then deleting all the attachments attached to that parent record 

Just replace the child record sys_id in the second parameter and it will attach the attachments

 

    var gr = new GlideRecord('x_pgpl2_spend_pre_contract');
    gr.addQuery('sys_id', current.sys_id);
    gr.query();
    if (gr.next()) {
        var attach = new GlideSysAttachment();
        attach.deleteAll(gr);
        attach.copy('x_pgpl2_spend_pre_contract_task','child_record_sys_id', 'x_pgpl2_spend_pre_contract', gr.sys_id);
    }

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

'child_record_sys_id'