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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can have business rule on sys_email table and include the specific file in the outbound email by inserting record into sys_email_attachment table

what's the issue with the above script?

Regards
Ankur

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

Can you give me a sample script please?

That mail script it is not working, no attachment is being added in the email. If I select the 'Include attachments' in notification, all existing attachment of a record is being added, but I only need 1 attachment that will come from the user who will attach in req/ritm 

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

Thank you for your idea. Can you share a script please for the #2? I assume it will be inside the IF condition.

 

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

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

if(grSysAtt.next()) {

//code to insert record into sys_email_attachment for the current sys_email record

}