Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to copy a single attachment?

peterraeves
Mega Guru

I'm trying to copy a single attachment. Why is my code not working?

//Create copy of attachment

var gsa = new GlideSysAttachment();

var fileName = attachment.getValue('file_name');

var contentType = attachment.getValue('content_type');

var content = gsa.getContentBase64(attachment);

gsa.write(toGR, fileName, contentType, content);

I tried using 'gsa.getContent(attachment)' for line 5 as well, but that did not work either. The result was always that an attachment was created to the correct record, with the correct name, but without any content. Am I not getting the content correctly?

Kind regards,

6 REPLIES 6

Ankur Bawiskar
Tera Patron

Hi Peter,



Use this code to copy single attachment from Record 1 of table A to Record 2 of table B


This single line of code will work



var sourceRecordSysId = '' // sys_id of Record 1 from Table A


var sourceTable = ''; // here for example it's table A


var targetTable   = '' // here for example it's table B


var targetRecordSysId = '' // sys_id of Record 2 from Table B



GlideSysAttachment.copy(sourceTable , sourceRecordSysId, targetTable , targetRecordSysId );



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

This does not copy a single attachment at all... It copies ALL attachments...


Hi Peter,



Do you want to copy only 1 attachment and that too based on some condition such as filename contains some word etc or attach the last attached attachment?



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I want to sync attachments between records.



So if the attachment is added to one record, it should be copied to the other record.



I first tried the above code, but that did not work. Now I'm trying brute force with



var gsa = new GlideSysAttachment();


  gsa.deleteAll(toGR);


  gsa.copy(fromTable, fromSysID, toTable, toSysID);



but it does not work perfectly either. I have no idea why, but somehow because of the business rule, the servicenow buffer gets messed up and the attachment is added twice.



To prevent the business rule from looping, I am using the following check



moreAttachments: function(attachment) {


  //Get all needed GlideRecords


  var fromTable = attachment.getValue('table_name');


  var fromSysID = attachment.getValue('table_sys_id');


  var fromGR = new GlideRecord(fromTable);


  fromGR.get(fromSysID);




  var field = (fromTable == 'incident' ? 'u_other' : 'u_incident');


  var toGR = fromGR[field].getRefRecord();


  var toTable = toGR.getTableName();


  var toSysID = toGR.getValue('sys_id');



  //Compare number of attachments


  var tables = [fromTable, toTable];


  var ids = [fromSysID, toSysID];


  var nums = [];


  for (var i = 0; i < tables.length; i++) {


  var agg = new GlideAggregate('sys_attachment');


  agg.addAggregate('COUNT');


  agg.addQuery('table_name', tables[i]);


  agg.addQuery('table_sys_id', ids[i]);


  agg.query();


  agg.next();


  nums.push(parseInt(agg.getAggregate('COUNT')));


  }




  return (nums[0] > nums[1]);


  },









If this is true, the business rule can run and delete and copy the attachments.