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

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

is there any way to send an attachment in notification without storing in existing records.

The short answer is no  the OOB notification engine always reads attachments from sys_attachment, so something has to be stored somewhere temporarily.

 

The cleanest workaround is attach → notify → delete:

// 1. Attach temporarily
var attachment = new GlideSysAttachment();
var attachSysId = attachment.write('your_table', recordSysId, 'report.csv', 'text/csv', csvContent);

// 2. Fire notification
gs.eventQueue('your.event.name', gr);

// 3. Delete after sending
var att = new GlideRecord('sys_attachment');
att.get(attachSysId);
att.deleteRecord();

Attach it to any existing record, fire the event, then clean it up right after. No custom table, no permanent storage. That's as close as you can get without going outside ServiceNow entirely .

 

Hope this helps!

 

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

 

Thanks,

Vishnu

Deletion part alone is not working. is there any alternative for deletion