Notification trigger when attachment is added to case

Stacy1
Mega Guru

Hello,

My use case:  Every time an attachment is added to a case by anyone accept for my integration user then send an email to a particular email address with the recent attachment.

I have found this article on the community on how to create the Script Include (getAttachments) and the Business Rule on the sys_email table.  https://community.servicenow.com/community?id=community_question&sys_id=acfa07addb5cdbc01dcaf3231f96...

I am not sure how this works.  How do I create a notification that gets that last attachment?  I need it to go to a specific email address.  

Do I need to create a mail script?  I am a little bit confused.  Any clarification or help would be appreciated.

Thanks,

Stacy

1 ACCEPTED SOLUTION

Stacy1
Mega Guru

This is to send ONLY the attachment that was just attached to the record.  In my case I want to send an email to our client when an attachment is attached and only that 1 attachment as they send us attachments and we only want to send those that we attach to the case not everything every time a new attachment is put on.

I have created a before insert Business Rule on the Sys Attachment table.

My condition is the table = sn_customerservice_case and the created by is NOT our integration user. (the one that sends us attachments from the customer).

I had to script the creation of the Notification and the Email Attachment.

(function executeRule(current, previous /*null when async*/ ) {

var fileName = current.file_name;
var tableName = current.table_name;
var tableSysId = current.table_sys_id;
if (tableName == 'sn_customerservice_case') {
var GR = new GlideRecord(tableName + '');
GR.addQuery('account', '8ef1ba261be30890ef6b866fdc4bcbf3');//Only send if this is our particular customer, we don't want this for all accounts.
GR.addNotNullQuery('u_client_case_id');//Only send if this field is true as this means it's an integrated case
GR.addQuery('sys_id', tableSysId + '');
GR.query();

if (GR.next()) {
var caseid = GR.u_client_case_id;

var gre = new GlideRecord('sys_email');
gre.initialize();
gre.mailbox = 'outbox';
gre.state = 'ready';
gre.target = current.sys_id;
gre.subject = "Case ID-" + caseid;
gre.recipients = 'bxxx.admin@xxx.com';
gre.body = "<p>Attached from XXX</p><p></p>";
gre.content_type = "text/html";
gre.headers = "X-ServiceNow-Source:Notification-6fbc13c0dbc41450fd899236db961987 \nX - ServiceNow - SysEmail - Version: 2 ";
var newsysid = gre.insert();

var gr = new GlideRecord('sys_email_attachment');
gr.initialize();
gr.attachment = current.sys_id;
gr.file_name = current.file_name.toString();
gr.source = 'notification';
gr.content_disposition = 'attachment';
gr.email = newsysid;
gr.insert();

}
}

})(current, previous);

View solution in original post

12 REPLIES 12

Priyanka Chandr
Mega Guru

Hi,

Refer below link that might help you

https://community.servicenow.com/community?id=community_question&sys_id=df510729db98dbc01dcaf3231f96...

Kindly mark it correct and helpful.

Thanks,

Priyanka

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

We already have a OOB event which triggers when attachment is added or deleted you can make use of that or else create your own event and use Script Action to trigger notifications.

Thanks,
Ashutosh

Hi,

I did create a notification using the event attachment.uploaded.  I saw the event in the event log but the notification did not go.  Also I only want the attachment that was just uploaded not all of them, can we do that?

Thanks,

Stacy

Hi,


See what we did:

1) Create event : attach.incident

We create a Business rule on attachment table as below:

var fileName = current.file_name;
var tableName = current.table_name;
var tableSysId = current.table_sys_id;
if (tableName == 'incident') {
var incGR = new GlideRecord(tableName + '');
//incGR.addActiveQuery();
incGR.addNotNullQuery('correlation_id');
incGR.addQuery('sys_id', tableSysId + '');
incGR.query();

if (incGR.next()) {
gs.eventQueue('attach.' + incGR.tablename, current, incGR.number, incGR.correlation_id);
}
}

//attach.' + incGR.tablename this is our event name which will trigger script action.

 

Then is that script action we have our logic and then we trigger other event for notification from there. If you want you can skip script action and use this event in BR to trigger notification.


Thanks,
Ashutosh

Hi,

Thanks that triggered my notification but I didn't get the attachment.

Am I missing something?

Thanks,

Stacy