---
original_url: "https://jace.pro/blog/export-to-csv-custom/"
format: markdown
ai_optimized: true
---

Export to CSV (Custom)# Export to CSV (Custom)

June 7, 2022 [gliderecord ](/tags/gliderecord/)[servicenow](/tags/servicenow/)

  Enable AI Animation**What is the post about?**

Exporting to a custom CSV format. There’s probably a better way to do this. Here’s a way I’ve done it.

**What things would help with writing the post**

`// Export to CSV 
var attachment = new GlideSysAttachment();
var data = "Number,Name\n"
var gr = new GlideRecord('incident');
gr.addQuery('active', 'true');
gr.query();
while(gr.next()){
	data += gr.number + "," + gr.caller_id.name + "\n";
}
incident = new GlideRecord('incident');
incident.get('85071a1347c12200e0ef563dbb9a71c1'); //Gets the record I want to attach the file too

var newFile = attachment.write(incident, 'Incidents.csv', 'text/csv', data);`
---
[View this page on GitHub](https://github.com/jacebenson/jace.pro/tree/main/./src/posts/2022/2022-06-06-export-to-csv-custom.md).

[Export to CSV (Custom)](https://jace.pro/blog/export-to-csv-custom/) [Jace Benson](https://jace.pro) ![Jace Benson](https://jace.pro/icon-512x512.png)

---

*This content is from Jace Benson's ServiceNow and tech blog at jace.pro*
*Original post: https://jace.pro/blog/export-to-csv-custom/*
