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

@akin9 Before copying the attachments using attahcment.copy you can use a GlideAggregate query to check if the attachment with the same name already exist on the target change_request record. 

Hello @Sandeep Rajput 

Thanks for the quick reply!

If possible, Can you pls share the sample script.thanks!

You can probably modify the code to copy only if the attachment with the same name doesnt existing in the change request.

 

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

// Check if there is already an attachment with same name. If yes, dont copy
var attapp =new GlideRecord('sys_attachment');
attapp.addQuery('table_name','sysapproval_approver');
attapp.addQuery('table_sys_id',current.sys_id);
attapp.query();
if (attapp.next())
{
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);
}
}
}

})(current, previous

 


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

Hello @SanjivMeher 

Thank you for the script.

Now the multiple times pasting issue is resolved but

new attachments are not copy and paste to the CR table.

After -insert/update BR.

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

// Check if there is already an attachment with same name. If yes, dont copy
var attapp =new GlideRecord('sys_attachment');
attapp.addQuery('table_name','sysapproval_approver');
attapp.addQuery('table_sys_id',current.sys_id);
attapp.query();
if (attapp.next())
{
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);
}
}
}

})(current, previous);

If you have multiple attachment, use a while loop

 

 

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

// Check if there is already an attachment with same name. If yes, dont copy
var attapp =new GlideRecord('sys_attachment');
attapp.addQuery('table_name','sysapproval_approver');
attapp.addQuery('table_sys_id',current.sys_id);
attapp.query();
while (attapp.next())
{
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);
fixDuplicateImages(chg.sys_id);
}
}
}

function fixDuplicateImages(chg_sys_id){
  var now_GR = new GlideRecord('sys_attachment');
  now_GR.addQuery('table_name','change_request');
  now_GR.addQuery('table_sys_id',chg_sys_id);
  now_GR.query();
  var lastID ='not_a_match';
  var lastFile ='not_a_match';
  while(now_GR.next()){
    var isDuplicate = (lastID == now_GR.table_sys_id)&&(lastFile == now_GR.file_name);
    lastID = now_GR.table_sys_id;
    lastFile = now_GR.file_name;
    gs.print(now_GR.table_sys_id + ' ' + now_GR.table_name + ' ' + now_GR.file_name + ' ' + now_GR.sys_created_on + ' ' + isDuplicate);
    if(isDuplicate)
      now_GR.deleteRecord();
  }
}

})(current, previous);

 


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