How to copy a single attachment?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 01:15 AM
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 02:04 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 02:17 AM
This does not copy a single attachment at all... It copies ALL attachments...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 02:21 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 02:29 AM
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.