Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created January 2, 2017 18:58
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save douglascayers/c733b60db61c290b95ff0047ab3d2432 to your computer and use it in GitHub Desktop.
Apex CSV Example. Note the use of String.escapeCsv() method
String csv = 'Id,Name\n';
for ( List<Account> accts : [ SELECT id, name FROM Account LIMIT 10 ] ) {
for ( Account acct : accts ) {
csv += acct.id + ',' + acct.name.escapeCsv() + '\n';
}
}
ContentVersion file = new ContentVersion(
title = 'accounts.csv',
versionData = Blob.valueOf( csv ),
pathOnClient = '/accounts.csv'
);
insert file;
System.debug( file );
@douglascayers
Copy link
Author

Code snippet for my blog post about the many ways to export Salesforce data to CSV: https://douglascayers.com/2016/03/20/salesforce-easy-ways-to-export-data-as-csv/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment