The CreatorCon Call for Content is officially open! Get started here.

Script is copying multiple times

akin9
Tera Contributor

Hello Experts,

We want to copy attachments from "sysapproval_approver" table to change request table.

i have created below GlideSysAttachment.copy code is working fine.

but issue is whenever record is inserted or updated copying all attachments every time.

Requirement1.If approver attachment should copy only one time.

Please support me on this

 

Table =sysapproval_approver

After insert or update BR.

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here

var chg = new GlideRecord('change_request');
if(chg.get(current.sysapproval)){
var attachment = new GlideSysAttachment();
attachment.copy("sysapproval_approver",current.sys_id,"change_request",chg.sys_id);
}

})(current, previous

 

1 ACCEPTED SOLUTION

@akin9 condition is correct. Please try with below code.

 

var sy = new GlideRecord('sysapproval_approver');
sy.get(current.table_sys_id);
var changeSysID = sy.sysapproval;

 

var ch = new GlideRecord('change_request');
ch.get(changeSysID);


var attachment = new GlideSysAttachment();
attachment.deleteAll(ch);


attachment.copy("sysapproval_approver", current.table_sys_id, "change_request", changeSysID);

View solution in original post

26 REPLIES 26

You sure, you want to delete the existing attachment from change request šŸ˜®? Below script is doing that.

 

var ch = new GlideRecord('change_request');
ch.get(changeSysID);

 

var attachment = new GlideSysAttachment();
attachment.deleteAll(ch);

Please mark this response as correct or helpful if it assisted you with your question.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @akin9 ,
I trust you are doing great.
Here's an enhanced version of your script:

(function executeRule(current, previous /*null when async*/) {

    // Check if the attachment has already been copied
    if (current.u_attachment_copied) {
        return; // Exit if already copied
    }

    var chg = new GlideRecord('change_request');
    if (chg.get(current.sysapproval)) {
        var attachment = new GlideSysAttachment();
        attachment.copy("sysapproval_approver", current.sys_id, "change_request", chg.sys_id);

        // Set a flag to indicate the attachment has been copied
        current.u_attachment_copied = true;
        current.update();
    }

})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello @Amit Gujarathi 

Thanks for the Quick reply!

I have tried the above but no luck.

Table name -sysapproval_approver

After insert,

Its not copying the attchment.

After insert/update,

Its copying all the existing attchments once again.

pls check

 

@akin9 have you tried above code provided by me?

Ankur Bawiskar
Tera Patron
Tera Patron

@akin9 

business rule should be after insert on sys_attachment

Condition: current.table_name == 'sysapproval_approver'

Script:

(function executeRule(current, previous /*null when async*/) {

// Add your code here
// delete all attachments to the source record

// then copy all

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader