Empty email attachments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2015 11:18 AM
When an approval email is sent for change requests, we would like any attachments on the change record to be included in the approval email. Checking the "include attachments" on the notification that sends the approval email does not work. I suspect this is becaus the attachments are not on the approval record, they are on the change record.
So, I wrote an insert business rule on the approval table that goes to the attachment table, gets the record that represents the change attachments for the current change record, I create a copy of this record, replacing the change record related values for the attachment record related values. When the approval email is sent it contains the attachments, however when I open the attachment (in this case a Word doc that contains both text and an image), it is blank.
Any suggestion on how I can resolve this? I have included the business rule script and a screen shot of the email below.
var currentChange = current.sysapproval; // the sys_id of the change record
var currentSysID = current.sys_id; // sys ID of the approval record; will need this for the insertion in the attachment table
// go to the attachment table and see if there are any records there for the current change record
var sa = new GlideRecord('sys_attachment');
sa.addQuery('table_sys_id',currentChange);
sa.query();
while (sa.next()) {
gs.log('Found an attachment record for change: ' + currentChange);
var nr = GlideRecord('sys_attachment');
nr.initialize();
nr.compressed = sa.compressed;
nr.content_type = sa.content_type;
nr.file_name = sa.file_name;
nr.size_bytes = sa.size_bytes;
nr.size_compressed = sa.size_compressed;
nr.table_name = 'sysapproval_approver';
nr.table_sys_id = currentSysID;
nr.insert();
gs.log('Created approval attachment record');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2016 09:00 AM
Hey Ric,
Creating a new attachment (or copying) requires two steps - creating a new record on the sys_attachment and copying all the records on sys_attachment_doc. The sys_attachment_doc records contain the attachment file data in 4K segments. Unless the second step is done, it is likely to result in an attachment with no content, which I guess is the case here.
After inserting nr into the sys_attachment, try duplicating all the records on the sys_attachment_doc table. Point the duplicate records to nr. The code would look something like below. I have not able to validate it, please test before use.
Let us know if it works.
.
..
...
....
nr.content_type = sa.content_type;
nr.file_name = sa.file_name;
nr.size_bytes = sa.size_bytes;
nr.size_compressed = sa.size_compressed;
nr.table_name = 'sysapproval_approver';
nr.table_sys_id = currentSysID;
var nrSysId = nr.insert();
if (!nrSysId) {
var saDoc = new GlideRecord('sys_attachment_doc');
saDoc.addQuery('sys_attachment', sa.sys_id);
while (saDoc.next) {
var nrDoc = new GlideRecord('sys_attachment_doc');
nrDoc.initialize();
nrDoc.sys_attachment = nrSysId; //Reference the duplicate records to the newly created sys_attachment record
nrDoc.data = saDoc.data;
nrDoc.length = saDoc.length;
nrDoc.position = saDoc.position;
nrDoc.insert();
}
}
gs.log('Created approval attachment record');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2016 09:23 AM
Hi Ric,
Have you tried to use the Copy Attachments API? This should handle all of the sys_attachment_doc stuff for you. You simply give it a source table and record sys_id to copy from as well as a target table and record sys_id to copy to.
Copy Attachments from Record to Record - ServiceNow Wiki
GlideSysAttachment.copy('sourcetable', 'sys_id', 'destinationtable', 'sys_id');
Hope this helps.
-Jose
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2016 10:05 AM
Hello Jose and Ajit, I tested both of your suggestions and both worked, but I will go with the API option as this is easier to maintain. Thank you both for your excellent assistance.
Ric