Only Last attachment to be sent in Email Notification
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2015 01:49 PM
Our system is set up with Cases that are being worked and resolved (like an IT Incident). An Email Notification is sent out on case resolution that includes Close Notes the case worker types in. On occasion, there is also an attachment that needs to go back to the requester however, there can be multiple attachments (pdf, excel, etc) linked to the Case and we only want the last attachment that the case worker adds to be sent out with the final Email Notification (Resolution) - not all attachments. This would prevent the case worker from sending an additional manual email with an attachment. What is the best way to build this?
We want:
1.) The last attachment the case worker adds to the case to be sent in the the Email Notification (Resolution)
2.) If the Case worker does not upload an attachment, then the Email Notification is sent without an attachment (even if attachments are in the case as they came in from the email on Case Create).
My other thought would be if there was a way to attach a file within the 'Closed Notes' section and only that attachment would be sent. Not sure if that is even possible.
Let me know if you have any suggestions or ideas.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2015 01:51 PM
Thanks Julian. BTW what table is the Business Rule running against in the examples posted by Edwin?
I tried his code on sys_email and sys_attachment and incident.
I am trying to control the attachments that are sent in an email notification triggered by an incident record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2015 02:20 AM
this is on the sys_email table
(excuse the number of gs.log lines that are there)
Line 46 is calling a script include. In essence it is passing the email address of the recipient being processed and this returns a true or false.
true means they need an attachment
false means they do not
If they are internal, they do not, if they are some external users they need an attachment
/
/gs.log('add attachments started');
// Initial variables
var strAdded = '';
var myRec = current.instance;
//gs.log('add attachments : ' + strAdded + ' : ' + myRec);
//gs.log('add attachments : ');
// get the members of the Recipients and create an array
var RecArr = current.recipients.split(',');
//gs.log('add attachments : ' + current.recipients);
// Search the attachment table for the attachments for this record
var SAgr = new GlideRecord('sys_attachment');
SAgr.addQuery('table_sys_id',myRec);
SAgr.query();
// loop until we have checked all attachments
while (SAgr.next()) {
//gs.log('add attachments : SAgr.sys_id : ' + SAgr.sys_id);
// for each attachment, we need to check each recipient
for (i=0;i<RecArr.length;i++)
{
//gs.log('add attachments : current.sys_id : ' + current.sys_id + ' : SAgr.sys_id : ' + SAgr.sys_id + ' : RecArr['+i+'] : ' + RecArr[i]);
//gs.log('add attachments now going to call script include or die');
//gs.log('add attachments send Attachment : ' + new GroupMLongConditionList().sendAttachment(RecArr[i]));
// check to see if we "fullAttachmentSend" via EmailAddress
// or via the Noticification in use - we check to see if is a 3rd party Notification
//gs.log('add attachment checking to see if we send due to email or notification');
if (new GroupMLongConditionList().sendAttachment(RecArr[i]) == true )
{
//gs.log('add attachments : Now looking at the u_attachment_ebonding table');
// check out table for this attachment and this user
uaegr = new GlideRecord('u_attachment_ebonding');
uaegr.addQuery('u_3rd_party',RecArr[i]);
uaegr.addQuery('u_attachment_sys_id',SAgr.sys_id);
/*
uaegr.addQuery('u_attachment_sys_updated_on',SAgr.sys_updated_on);
uaegr.addQuery('u_attachment_content_type',SAgr.content_type);
uaegr.addQuery('u_attachment_file_name',SAgr.file_name);
uaegr.addQuery('u_attachment_size_bytes',SAgr.size_bytes);
uaegr.addQuery('u_attachment_size_compressed',SAgr.size_compressed);
*/
uaegr.query();
//gs.log('add attachments uaegr has been queried');
// if we do not find a match, we have not sent an email
// update the table so we do not send this attachment again
if (!(uaegr.next()))
{
//gs.log('add attachments, nothing found in uaegr');
uaegr.initialize();
uaegr.u_3rd_party = RecArr[i];
uaegr.u_attachment_sys_id = SAgr.sys_id;
/*
uaegr.u_attachment_sys_updated_on = SAgr.sys_updated_on;
uaegr.u_attachment_content_type = SAgr.content_type;
uaegr.u_attachment_file_name = SAgr.file_name;
uaegr.u_attachment_size_bytes = SAgr.size_bytes;
uaegr.u_attachment_size_compressed = SAgr.size_compressed;
*/
uaegr.insert();
// Add this attachment to a list of attachments already added
//gs.log('add attachments : strAdded : ' +strAdded);
// If this attachment has not been added to this email, then add it, otherwise, why add again ?
// need this as this loops based on each recipient and if there is more than one to receive.
if (strAdded.indexOf(SAgr.sys_id) == -1)
{
//gs.log('add attachments adding : ' +SAgr.table_name + ' : ' + SAgr.sys_id + ' : ' + SAgr.file_name + ' : ' + SAgr.size_bytes + ' : to : ' +current.instance + ' : ' + current.sys_id);
var table = SAgr.table_name;
//gs.print ('Looking for attachment');
// where gr.sys_id is the sys_id on the attachment table and table = u_client_impact_record
// the line below copies all the attachments from the parent record, not the selected attachments.
//GlideSysAttachment.copy(SAgr.table_name,SAgr.sys_id,'sys_email',current.sys_id);
var SAIgr = new GlideRecord('sys_attachment');
SAIgr.initialize();
SAIgr.table_name = 'sys_email';
SAIgr.table_sys_id = current.sys_id;
SAIgr.file_name = SAgr.file_name;
SAIgr.compressed = SAgr.compressed;
SAIgr.content_type = SAgr.content_type;
SAIgr.size_byes = SAgr.size_bytes;
//SAIgr.size_compressed = SAgr.size_compressed;
SAIgr.insert();
// now we need to get over to the sys_attachment_doc table and copy each record
// that is associated with the old attachment to the new one
var SADgr = new GlideRecord('sys_attachment_doc');
var SADIgr = new GlideRecord('sys_attachment_doc');
SADIgr.initialize();
SADgr.addQuery('sys_attachment.sys_id',SAgr.sys_id);
SADgr.query();
while (SADgr.next())
{
SADIgr.data = SADgr.data;
SADIgr.length = SADgr.length;
SADIgr.position = SADgr.position;
SADIgr.sys_attachment = SAIgr.sys_id;
SADIgr.insert();
}
strAdded += SAgr.sys_id;
//gs.log('add attachments : strAdded : ' +strAdded);
}
}
else
{
//gs.log('add attachments : matched a record so no send');
}
}
}
}