Need help in Email script in Notification Attachment

Ljone
Giga Contributor

Hi All,

Need your help. I have a requirement to send out a notification with attachment based on attachment of user to REQ/RITM. The scenario is, The user attaches a file to REQ/RITM starting with 'PO-COR' file name. There is a notification that should sent out that includes the physical attachment (not link) of what they attached. It should also not include other existing attachments of that record, only the one with PO-COR file, so I cannot use include attachments in Notification.  

What I did so far are:

1. Created event in event registry

2. Created notification in REQ/RITM called by the event in #1 this is fired only when that file name is inserted in sys_attachment table

   Include Attachments: not selected (if I select this all attachments are being picked up, I only need that 1 PO-COR file)

3.Created Mail script that is being called in the  Notification body, so far here is my code, and not working. IT DOES NOT ATTACH ANYTHING TO THE EMAIL. KINDLY HELP!!

I tweaked the code from Drew in this thread because mine is not static value, it will be dependent to the one user attaches to REQ/RITM  https://community.servicenow.com/community?id=community_question&sys_id=d8de774adbf25780e0e80b55ca961963

 

var grSysAtt = new GlideRecord('sys_attachment');
grSysAtt.orderByDesc('sys_created_on'); //sort with created descending
grSysAtt.addQuery('table_sys_id', current.sys_id);
grSysAtt.addQuery('file_name', 'STARTSWITH', 'PO-COR');
grSysAtt.setLimit(1); // pick only 1 attachment record
grSysAtt.query();

while(grSysAtt.next()) {

var myStringArray = grSysAtt.sys_id;

//This if statement checks the attachments that you used in myStringArray and makes sure that it only grabs those attachments.


if(myStringArray.getRowCount()== 1){
var content = new GlideSysAttachment().getContentStream(grSysAtt.sys_id);
new GlideSysAttachment().writeContentStream(current, grSysAtt.getValue('file_name'), grSysAtt.getValue('content_type'), content);
template.print('test');
}
}

 

 

@Ankur Bawiskar @Allen Andreas  KINDLY HELP ME. THANK YOU!

1 ACCEPTED SOLUTION

Hi,

you can have after insert BR on sys_email

Condition: Use your valid condition for your table name

Script:

1) query sys_attachment to fetch the correct record for your user

2) then insert record into sys_email_attachment for the current sys_email record

3) the file would then be included in the outbound email

Note: Keep Include attachment unchecked and test once

Regards
Ankur

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

View solution in original post

7 REPLIES 7

Ljone
Giga Contributor

I updated the script and looks like working! I am doing some more testing

 Thank you for your help @Ankur Bawiskar 

 

var origin = current.instance; //sysid of the ritm/req that fired the email

gs.log("test1 " + rec);
var grSysAtt = new GlideRecord('sys_attachment');
grSysAtt.orderByDesc('sys_created_on'); //sort with created descending
grSysAtt.addQuery('table_sys_id', origin);
grSysAtt.addQuery('file_name', 'STARTSWITH', 'PO-COR');
grSysAtt.setLimit(1); // pick only 1 attachment record
grSysAtt.query();

if(grSysAtt.next()) {
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = grSysAtt.sys_id;
rec.file_name = grSysAtt.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();

@Ljone 

Glad to know.

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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

Michael_Martin
Tera Contributor

Actually, you can have it much easier.
In the Notification you can set "Include attachments" under "What will it contain".

You will be able to change this setting via email_script with whatever condition you want.
May it be from a property or a value of your current record.

In the mailscript.. simply oversteer it..
you dont need two notifications.

 

email_action.include_attachments = true;
or
email_action.include_attachments = false;

 


This field is not documented by ServiceNow, but with email_action you have access to all fields from your notification. Therefore, a bit of creative testing and voila.. you have something.