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

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

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);
}

 

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

Tanushree Maiti
Kilo Patron

Hi @Mohamed_Hazik 

 

refer this Servicenow Documentation: 

Document attachments on an email notification  //Sample code is there

Generate CSV file through script

 

Also check:

https://www.servicenow.com/community/developer-forum/how-to-send-attachment-in-an-email-notification...

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Ankur Bawiskar
Tera Patron

@Mohamed_Hazik 

you are sending email?

So it must be from some record?

is that email a group email address?

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