Using GlideSysAttachment.copy only copies one attachment when there are multiple on the source record

tamarasylte
Mega Expert

I have a requirement to copy a Knowledge article into a Change Task, including any attachments that might be on the article.

Here is the business rule:

//   function onDisplay(current, g_scratchpad) {                      // When to run: Display           // Order: 100           // Filter Conditions: Special Instructions Flag is true          //gs.log('InforDebug BSR: In Business Rule');                      gs.log('InforDebug BSR: kb sys id is -- '+ current.u_infor_spec_instructions);           gs.log('InforDebug BSR: html text is -- '+ getKBhtml(current.u_infor_spec_instructions));                      var tblName = current.getTableName(); // get table name of record currently running against (allows reusability)           var manageAttach = new InforManageAttachments(); // create new instance of scriptInclude               current.u_infor_spec_instructions_display = manageAttach.copyHTMLFromKB(current.u_infor_spec_instructions, tblName); // call function to copy HTML and atatchments to record of tblName           manageAttach.deleteDups(tblName); // call function to remove duplicate attachments from record of tblName   }

and the script include

var InforManageAttachments = Class.create(); InforManageAttachments.prototype = Object.extendsObject(AbstractAjaxProcessor, {                      copyHTMLFromKB : function(kbID, tblName) {                     var kbRec = new GlideRecord('kb_knowledge');                     if (kbRec.get(kbID)) {                               GlideSysAttachment.copy('kb_knowledge', kbRec.sys_id, tblName, current.sys_id);                                         // Added 4/21/2016 - Tamara - copy all attachments from KB record to CTASK                               return kbRec.text;                     } else { return ''; }           },                        deleteDups : function(tblName) {                     var grSysAttach = new GlideRecord('sys_attachment');                         grSysAttach.addQuery('table_name', tblName); //substitute your table name here                         grSysAttach.addQuery('table_sys_id',current.sys_id);                         grSysAttach.orderByDesc('sys_created_on');                         grSysAttach.query();                         var lastID = 'not_a_match';                         var lastFile = 'not_a_match';                         while (grSysAttach.next()) {                                   gs.log("scriptInclude - Processing sys_attachment record " + grSysAttach.sys_id + " and table_sys_id " + grSysAttach.table_sys_id);                               var isDuplicate = (lastID == grSysAttach.table_sys_id) && (lastFile == grSysAttach.file_name);                                   lastID = grSysAttach.table_sys_id;                                   lastFile = grSysAttach.file_name;                                   if (isDuplicate)   {                                         grSysAttach.deleteRecord();                                             gs.log(grSysAttach.table_sys_id + ' ' + grSysAttach.table_name + ' ' + grSysAttach.file_name + ' ' + grSysAttach.sys_created_on + ' ' + isDuplicate);                                   }                     }                     return;           },           type: 'InforManageAttachments' });

The consistent behavior is that only one attachment is copied from the Knowledge article to the Change Task, even though multiple attachments are added to the Knowledge article.   I used the documentation from Useful Attachment Scripts - ServiceNow Wiki   and Copy Attachments from Record to Record - ServiceNow Wiki.

1 ACCEPTED SOLUTION

I worked with support and had to rewrite the script I was using that copied duplicates. I don't think the underlying copy was fixed.


View solution in original post

16 REPLIES 16

Chuck Tomasi
Tera Patron

You're going to need to do a query to get all the attachments for that record first, then call GlideSysAttachment.cop() for each attachment found.


I take that back... the documentation clearly says:



Note: GlideSysAttachment.copy copies all attachments; it cannot select specific attachments.



Something sounds buggy. What release of ServiceNow are you using?


We just moved to Geneva.


I can do that - but the documentation says



Script:


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


Note


Note: GlideSysAttachment.copy copies all attachments; it cannot select specific attachments.

Is this an error in the documentation then?