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
sunny13
Giga Expert

Hi Mike,


can you take a look at Copy attachment ??



I'm looking out for a solution


shashankverma05
Tera Contributor

You can use the below code of snippet the update the records



var email_log = new GlideRecord('sys_email');


email_log.query('uid', email.uid);


email_log.orderByDesc('sys_created_on');


email_log.query();


if(email_log.next()) {


var dS= new GlideRecord('sys_data_source');


if (dS.get('sys_id_of_datasource')) {


  var attachment = new GlideSysAttachment();


  attachment .deleteAll(dataSource);// will delete attachment


attachment.copy("sys_email", sys_email.sys_id, 'sys_data_source', dataSource.sys_id); // Copy attachment from email to data source


  var ImportSet = new GlideRecord('scheduled_import_set'); // Trigger the Scheduled Import Set


  if (ImportSet.get('164e2c614f231200c233eb118110c789')) {


  ImportSet.run_as = 'system';


  ImportSet.update();


  SncTriggerSynchronizer.executeNow(ImportSet);


  } else {


  gs.logError('Import Error');


  }


} else {


  gs.logError('Error);


}


}


sunny13
Giga Expert

Hey thanks .


David M1
Tera Contributor

Hi there. This looks exactly what I am looking for, but I'm not sure how to implement it, I'm only a month into ServiceNow (Kingston) and learning fast.

I have created a business rule for the sys_attachment table, set it to insert and update. I can't see how it gets fired through from the new_call. I know I'm missing something simple.

Could you please add some how to implement instructions?

I'd appreciated any help

Thanks

David

Marcin Skiba
Tera Contributor

I tried to copy just one attachment, so GlideSysAttachment.copy wasn't good in my case. I end up with very similar script but preview in 'Activity' was screwed up. Your solution after little modification is doing the job perfectly. Thanks