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

I want to copy attachments from variable to variable in RITM with different sys_id

MiY
Tera Contributor

I have created the following Business Rule that copies attachments from variable to variable in RITM.

MiY_0-1695178511502.png

MiY_1-1695178522454.png

 


However, with this method, attachments are created with the same sys_id, and when I update the attachment of the source variable or the destination variable, the attachment of the other variable is deleted as shown in the following screenshot.

MiY_2-1695178709186.png

 


Is there a good way to prevent this from happening, I need to copy the attachment with a different sys_id?

1 REPLY 1

Danish Bhairag2
Tera Sage

Hello @MiY ,

 

Could you please try with the below code, if it works for you. Might not be the best standard way of writing it, but it works.

 

 

 

var id = 'sysid1,sysid2,sysid3';//hardcode variables sys Id as comma seperated/store it in property as comma seperated value where attachment will be present 
var attachID = 'sysid1,sysid2,sysid3';//hardcode variables sys Id as comma seperated/store it in property as comma seperated value where attachment needs to be copied.
var value = '';

id = id.split(',')
attachID = attachID.split(',');

for(var i = 0;i< id.length;i++){
var gr = new GlideRecord('sc_item_option_mtom');
gr.addEncodedQuery('request_item='+current.sys_id+'^sc_item_option.item_option_new='+id[i]);// current.sys_id = RITM sys id & id[i] = variable (which is created in catalog item/ record producer) sys id where attachment is present 
gr.query();
if(gr.next()){
    var attach1 = new GlideRecord('sc_item_option');
    attach1.addQuery('sys_id',gr.sc_item_option);
    attach1.query();
    if(attach1.next()){
        value = attach1.value;
        var gd = new GlideRecord('sc_item_option_mtom');
        gd.addEncodedQuery('request_item='+current.sys_id+'^sc_item_option.item_option_new='+attachID[i]);// current.sys_id = RITM sys id & attachID[i] = variable (which is created in catalog item/ record producer) sys id where attachment is suppose ot be copied
        gd.query();
        if(gd.next()){
            var attachfile = new GlideRecord('sc_item_option');
             attachfile.addQuery('sys_id',gd.sc_item_option);
             attachfile.query();
             if(attachfile.next()){
                attachfile.value = value;
                attachfile.update();
             }
        }
    }
}
}​

 

 

Please mark my answer helpful & solution accepted if it resolves your query.

 

Thanks,

Danish