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

Hello @SanjivMeher @Sandeep Rajput 

We have tried the above code but no luck.

its not pasting the attachment and making instance as slow.

and we are getting like below multiple uploading image at same time?

can you pls check and correct me.

 

akin9_0-1701326376414.png

 

Ok. I dont think it is going to work, because the copy function copies everything from one record to another. And in you case there could be one or more attachments.

The other option would be to creating a relationship. Please refer the below link and refer the section 'All Request task and Approval Attachments'. That way you dont have to copy attachment. Everytime an attachment is attached, it can be seen in a related list in the parent record

https://servicenowguru.com/system-definition/relationships/defined-related-lists/

 


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

dhanu3
Kilo Sage

@akin9 Please use after insert BR on attachment table and select in condition table is Approval table and then write your code

akin9
Tera Contributor

Hello @dhanu3 ,

Thanks for the quick reply,

I have tried but no luck pls correct me on more details.thanks!

 

akin9_0-1701328895957.png

 

Below code provided by Sanjiv should work. Please check how you are fetching change request sys id. As now you have BR on attachment table it will give you approval record sys id as table sys id. You have to fetch change request sys id.

 

var chg = new GlideRecord('change_request');
if(chg.get(current.sysapproval)){
var attchg = new GlideRecord('sys_attachment');
attchg.addQuery('table_name','change_request');
attchg.addQuery('table_sys_id',chg.sys_id);
attchg.addQuery('file_name',attapp.file_name)
attchg.query();
if (!attchg.next())
{
var attachment = new GlideSysAttachment();
attachment.copy("sysapproval_approver",current.sys_id,"change_request",chg.sys_id);
}
}