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

SanjivMeher
Kilo Patron
Kilo Patron

If you want to copy the attachment only on insert, It should be an after Insert BR.

 

If you want to copy attachment, when the approval is approved, you should have an after Update BR, with condition state changed to Approved.


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

Hello @SanjivMeher 

Thanks for the quick reply!

Issue

1.If  approver attached attachment its pasting on the CR table.

 and whenever i added a comment all the time same attachment is attaching .

Its "like comment - attachment - repeat"

May be because you dont have a condition in the Business Rule. 

It should only copy when the state changes to Approved.


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

Hello @SanjivMeher ,

copy when the state changes to Approved is working 

but after insert is not working?

Is it possible to achieve copy and paste attachment only one time?

whether its open state?

please help on this.thanks!