generating CSV attachment and sending via email notification

Mohamed_Hazik
Tera Contributor

I have a requirement to convert a list of records from a table into an Excel/CSV file and send it as an attachment through an email notification in ServiceNow. I am looking for the best practice approach for handling the attachment and notification process.

Do I need to create a separate custom table for storing and tracking the generated attachments. What would be the recommended approach for implementing this?

1 ACCEPTED SOLUTION

Vishnu-K
Kilo Sage

Hi @Mohamed_Hazik ,

 

No, you don't need a custom table. Just attach the CSV to any existing record using GlideSysAttachment and fire a notification from there.

 

Step 1 – Generate the CSV string

var gr = new GlideRecord('your_table');
gr.addEncodedQuery('your_query');
gr.query();

var csv = 'Field1,Field2,Field3\n';
while (gr.next()) {
    csv += gr.getValue('field1') + ',' + gr.getDisplayValue('field2') + '\n';
}

Reference: https://www.servicenow.com/community/developer-articles/generate-csv-file-through-script/ta-p/231977...

 

Step 2 – Attach it to a record

var attachment = new GlideSysAttachment();
attachment.write(tableName, recordSysId, 'report.csv', 'text/csv', csv);

This lands in sys_attachment against that record — no custom table needed.

 

Step 3 – Trigger the notification

Don't use GlideEmailOutbound — it doesn't support attachments (KB0789188):

 

https://www.servicenow.com/community/itsm-forum/glideemailoutbound/m-p/3084137

 

Instead, fire an event and use a standard Email Notification with "Include Attachments" checked. The notification engine picks up the attachment automatically.

gs.eventQueue('your.custom.event', gr, param1, param2);

 

Reference: https://servicenowguru.com/scripting/send-email-notification-attachments/

 

Full working thread with the same pattern: https://www.servicenow.com/community/developer-forum/how-to-email-csv-file-from-server-side-script-r...

 

Hope this helps!

 

If it helped you please do mark it as helpful and accept the solution

 

Thanks,

Vishnu

View solution in original post

12 REPLIES 12

Yes, I am sending an email for group

@Mohamed_Hazik 

then why not use scheduled report and link a report to it and keep that group email in recipient

That report will have a table associated to it

In scheduled report you can select PDF format

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Laveena-Agarwal
Mega Sage

Hi @Mohamed_Hazik -

 

I have a solution in mind, but I have a few queries:

  1. On which table do you want to configure this email notification? Or is there no restriction on the table?
  2. What should be the execution frequency for this automation?
    Should it run daily, weekly, monthly, etc.? Or is there any specific trigger involved?
    For example, when a button is clicked, should the records get converted into an Excel file and sent through a notification?
  3. Do you want the notification to be sent only to email addresses/users that exist in ServiceNow?