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

After talking with an SC who tested it, the GlideSysAttachment.copy() simply clones the attachment over so it will double the storage space used.


Not a best way to do it, but here is a work around that I am using. Might need to alter some code, but the method is there. It seems like it's working for me so far. This will delete all current attachments on the request and re-add the attachments from the catalog task table.



Business Rule


When: After - Insert


Table: Attachment [sys_attachment]



function onAfter(current, previous) {


        var catalog = new GlideRecord('sc_task');


        catalog.addQuery('sys_id', current.table_sys_id);


        catalog.query();


        if (catalog.next()){


                  var reqItem = new GlideRecord('sc_req_item');


                  reqItem.addQuery('sys_id', catalog.request_item);


                  reqItem.query();


                            if (reqItem.next()){


                                      var req = new GlideRecord('sc_request');


                                      req.addQuery('sys_id', reqItem.request);


                                      req.query();


                                                if (req.next()){


                                                          deleteDuplicateAttachments(req.sys_id);


                                                          GlideSysAttachment.copy('sc_task', catalog.sys_id, 'sc_request', req.sys_id);


                                                }


                            }


        }


        function deleteDuplicateAttachments(requestID) {


                  var gr = new GlideRecord('sys_attachment');


                  gr.addQuery('table_name', 'sc_request');


                  gr.addQuery('table_sys_id', requestID);


                  gr.query();


                  while (gr.next()) {


                            gr.deleteRecord();


                  }


        }


}


var call = new GlideRecord('new_call');


call.addQuery('sys_id', current.table_sys_id);


call.addQuery('transferred_to', '!=', '');


call.query();


if(call.next()){


  var att = new GlideRecord('sys_attachment');


  att.initialize();


  att.file_name = current.file_name;


  att.content_type = current.content_type;


  att.compressed = current.compressed;


  att.table_name = call.call_type;


  att.size_bytes = current.size_bytes;


  att.size_compressed = current.size_compressed;


  att.table_sys_id = call.transferred_to;


  var attRec = att.insert();



  var attDoc = new GlideRecord('sys_attachment_doc');


  attDoc.addQuery('sys_attachment', current.sys_id);


  attDoc.query();


  while(attDoc.next()){


  var attDocCopy = new GlideRecord('sys_attachment_doc');


  attDocCopy.initialize();


  attDocCopy.sys_attachment = attRec;


  attDocCopy.position = attDoc.position;


  attDocCopy.length = attDoc.length;


  attDocCopy.data = attDoc.data;


  attDocCopy.insert();


  }


}



This is the way I copied attachments on an existing call record over to incident.   This is so the call records could live and the end user could simply refer to it when adding things.


How to copy only some selected attachments from incident table to email table. Can you help me with that.


Hey Mike, can you give more description on this BR? I am planning to do a 'New Call'   <-> 'Sc_Request'   <-> 'sc_req_item' tables and was thinking on the same lines...