- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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';
}
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
19 hours ago
Put that deletion part in the try-catch block and check the logs to verify what error is it, then we can resolve it accordingly.
Regards,
Vishnu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
18 hours ago - last edited 18 hours ago
Thanks Vishnu. When I attempt to delete the attachment immediately after triggering the event, it also removes the attachment from the email notification. The email is being sent without the attachment.
var headers = ["Number", "Full Name", "Current City", "Candidate Status", "Job Title", "Business Reviewer", "Expected CTC"];
var fileName = 'Pending Candidate List.csv';
var csvData = '';
csvData += headers.join(",") + "\r\n";
var gr = new GlideRecord("table_name");
gr.addEncodedQuery('candidate_status=pending');
gr.query();
while (gr.next()) {
csvData += '"' + gr.number + '",';
csvData += '"' + gr.full_name + '",';
csvData += '"' + gr.current_city + '",';
csvData += '"' + gr.candidate_status + '",';
csvData += '"' + gr.job_title + '",';
csvData += '"' + gr.business_reviewer.getDisplayValue() + '",';
csvData += '"' + gr.expected_ctc + '",';
csvData += '"",';
csvData += "\r\n";
}
var grRec = new GlideRecord("table_name");
if (grRec.get("1828f7c893688350bf73f6627cba10b3")) {
var grAttachment = new GlideSysAttachment();
var attachmentSysId = grAttachment.write(grRec, fileName, 'text/csv', csvData);
gs.eventQueue('event_name', grRec, attachmentSysId, "MAIL ID");
}
try {
var gsa = new GlideSysAttachment();
gsa.deleteAttachment(attachmentSysId);
gs.info("Attachment deleted successfully: " + attachmentSysId);
} catch (ex) {
gs.info("Error while deleting attachment: " + ex.message);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
17 hours ago
gs.eventQueue() works asynchronously so then we cannot use delete there, that leaves us to write a cleanup schedule that will delete the attachments.
Hope this helps!
If it helped you please do mark it as helpful and accept the solution
Thanks,
Vishnu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
refer this Servicenow Documentation:
Document attachments on an email notification //Sample code is there
Generate CSV file through script
Also check:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
19 hours ago
you are sending email?
So it must be from some record?
is that email a group email address?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
