Copy Attachment with out duplicates

nayeem2
Giga Expert

Hi All,

 

I am trying to copy attachment from task table to request with out duplicates

 

can any one help me

 

thanks in advance

35 REPLIES 35

harikrish_v
Mega Guru

Hi Nayeem,



What do you mean by without duplicates? I you want to copy attachments from record to record, you can use this in any server side script:-



GlideSysAttachment.copy('sourcetable', 'sys_id', 'destinationtable', 'sys_id');




Thanks & Regards,


Hari


Hi Hari,



Actual scenario is when I attach a attachment to request I am able to copy the attachment to task but if I attach other attachment to request both previous one and current attachments both are copying so that in task I am getting one attachment two times can you please help me



Thanks & Regards


Nayeem


You can use the below code inside an after insert business rule on the sys_attachment table to remove duplicates:-



var gr = new GlideRecord('sys_attachment');


      gr.addQuery('table_name', 'LIKE', '<your table name>'); //substitute your table name here


      gr.orderBy('table_sys_id');


      gr.orderByDesc('sys_created_on');


      gr.query();


      var lastID = 'not_a_match';


      var lastFile = 'not_a_match';


      while (gr.next()) {


              var isDuplicate = (lastID == gr.table_sys_id) && (lastFile == gr.file_name);


              lastID = gr.table_sys_id;


              lastFile = gr.file_name;


              gs.print(gr.table_sys_id + ' ' + gr.table_name + ' ' + gr.file_name + ' ' + gr.sys_created_on + ' ' + isDuplicate);


              if (isDuplicate)


                      gr.deleteRecord();


      }


Thanks & Regads,


Hari


How does ServiceNow handle the data attachments? If it knows one's been copied from the Catalog Task to the Approval or RITM (by use of the GlideSysAttachment.copy()), does it actually keep that many copies of the same attachment or does it just modify the linkages to give access to that same file in multiple ways?