Need help in sending attachment in email notification

divyalakshmi
Tera Guru

Hi All,

I have a requirement to dynamically generate and send an attachment in an email notification in scoped app. To do this, I have generated the pdf attachment which customer needs using script and attaching the same to the current record which is must as per requirement. Post attachment generation  am triggering an event to fire an email notification in which this generated attachment file should include as attachment. "Include attachments" option doesn't work in this scenario as we need to send only this particular attachment in the email. There will be multiple attachments attached in the record so i need to exclude them and include only the generated pdf through script to get attached. Any inputs will be really helpful. 

 

Thanks,

Divya

2 REPLIES 2

Mahak2
Kilo Guru

Hello,

 

Can you please check below code:

//These are the sys_ids of the attachments that you want to attach to the email.  You can find the sys_id by right-clicking the attachment on the attachment table and copying the sys_id.
var myStringArray = "57a856841ba937007b1ca7d4bd4bcb45,eba856841ba937007b1ca7d4bd4bcb46";

//This field wasn't used so I commented it out
//var att =[];

//Current.Instance returned undefined for me in my logging.  I attached both of my attachments to the email notification that I wanted to use.  Then I went to the sys_attachments table and found both of my attachments.  For those attachments it showed that the table sys ID was 3d68b54c1b2937007b1ca7d4bd4bcb47 so I substituted the variable sys for that sys_id.
//var sys = current.instance;
var sys = "3d68b54c1b2937007b1ca7d4bd4bcb47";

//This queries the sys_attachment table for all attachments where the table_sys_id is the sys_id you used above.
var grSysAtt = new GlideRecord('sys_attachment');
grSysAtt.addQuery('table_sys_id',sys);
grSysAtt.query();
while(grSysAtt.next()) {
var asys = 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.indexOf(asys)>=0){
var content = new GlideSysAttachment().getContentStream(grSysAtt.sys_id);
new GlideSysAttachment().writeContentStream(current, grSysAtt.getValue('file_name'), grSysAtt.getValue('content_type'), content);
gs.log("test name" + grSysAtt.getValue('file_name'));
}
}

 

Kindly check and let me know if this helps.

Mark my response as helpful and correct as applicable.

sisf
Tera Contributor

@divyalakshmi I have a similar requirement , please help with steps to achieve this functionality.