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

Hi, Can you share what they solutioned to you? I have the same issue and would love to know how to copy the attachments. What did Support give you?


Thanks


The full solution we use is a set of fields to specify import of KB contents to a task, or other record, backed up by a business rule (per table where this is used) and scriptInclude to do the processing.



Fields:



Special Instructions Flag (boolean)


Special Instructions (Reference field to kb_knowledge)


Special Instructions Display (html)



find_real_file.png



Business rule for any table where the KB copy is enabled:


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   } 

The script include is as follows:


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' });

Did you get a problem number/ fix reference number for this bug? If so can you please share?


I did not ever get a PRB for the issue, so have been using the scripting shown in above comments.


      GlideSysAttachment.copy() will copy all attachments. What you are missing is how to access the array of results.



GlideSysAttachment.size() is an important method here, as it tells you the size of the (java) arraylist and you can use it to loop through the results.  



Example:



  var myEmailClientAttachments = GlideSysAttachment.copy(current.getTableName(), current.sys_id, targetTable, myCurrentCase);//returns ArrayList map = new ArrayList();



var allNewAttachments = [];



      for (var i = 0; i < myEmailClientAttachments.size(); i++) {


          var str = myEmailClientAttachments.get(i);


          var arr = str.split(',');


          gs.log("Attachment found from email client: " + arr[1],'emailClientAttachments');


          allNewAttachments.push(arr[1]);


      }



If this resolved your issue please mark it as solved or mark this as helpful. Thanks!