Mike Allen
Mega Sage

The use case was to allow users to have the call record as their main entry point.   So, anytime they want to attach something to their case, they would attach it to the call.   This attachment does not copy over to the related incident.   I scripted a business rule that sits on sys_attachment to do the copy.

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();

      }

}

It should be said that there is a method in GlideSysAttachment to copy (GlideSysAttachment.copy('sourcetable', 'sys_id', 'destinationtable', 'sys_id');).   However, it is flawed for my purposes.   It copies every attachment sitting on a particular record over to another record.   Calling it within a script will copy, and the next time it is called, it copies everything, including that which it has already copied, creating duplication.

5 Comments