Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Created September 8, 2018 13:43
Show Gist options
  • Select an option

  • Save SalesforceBobLightning/f48ac455ca66467165cde4c07b5428ca to your computer and use it in GitHub Desktop.

Select an option

Save SalesforceBobLightning/f48ac455ca66467165cde4c07b5428ca to your computer and use it in GitHub Desktop.
Export SObjects Fields as CSV files
public class ExportSObjectAsCSV {
// update with lowercase object names that you want to export as CSV files
private static Set<String> sObjectsToExport = new Set<String> {'account', 'appointment__c', 'triages__c'};
public static void Execute() {
System.debug('Starting: ExportSObjectAsCSV');
System.debug('SObjects to Export As CSV:');
for(String sobjectname : sObjectsToExport) {
System.debug(sobjectname);
}
StringBuffer buffer;
Map<String, String> fileMap = new Map<String, String>();
Map<String, Schema.SObjectType> SObjectMap = Schema.getGlobalDescribe();
System.debug('Starting: Processing SObjects: ' + SObjectMap.size());
for (String sobjectname : SObjectMap.keySet()) {
System.debug(sobjectname);
if (sObjectsToExport.contains(sobjectname) == false) continue;
Schema.DescribeSObjectResult sObjectResult = Schema.getGlobalDescribe().get(sobjectname).getDescribe();
Map<String,Schema.SObjectField> fields = sObjectResult.fields.getMap();
buffer = new StringBuffer();
buffer.append('Object, ');
buffer.append('Name, ');
buffer.append('Type\n ');
System.debug('Starting: Processing Fields: ' + fields.size());
for (String fieldName : fields.keySet()) {
System.debug(fieldName);
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
buffer.append(sobjectname + ', ');
buffer.append(fieldResult.getLabel() + ', ');
buffer.append(String.valueOf(fieldResult.getType()) + '\n');
}
System.debug('Ended: Processing Fields');
fileMap.put(sobjectname, buffer.toStr());
}
System.debug('Ended: Processing SObjects');
List<ContentVersion> contentVersionList = new List<ContentVersion>();
System.debug('Creating ContentVersion Records...');
for (String filename : fileMap.keySet()) {
ContentVersion contentVersion = new ContentVersion();
contentVersion.versionData = Blob.valueOf(fileMap.get(filename));
contentVersion.title = filename;
contentVersion.pathOnClient = '/' + filename +'.csv';
contentVersionList.add(contentVersion);
}
System.debug('Inserting ContentVersion records: ' + contentVersionList.size());
// insert records. Attached to current users profile.
insert contentVersionList;
System.debug('Ended: ExportSObjectAsCSV');
}
}
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
global class StringBuffer {
private String theString;
global StringBuffer(){
this('');
}
global StringBuffer(String str){
theString = str;
}
global StringBuffer(Decimal d){
theString = '' + d;
}
global StringBuffer(Double d){
theString = '' + d;
}
global StringBuffer(Long l){
theString = '' + l;
}
global StringBuffer(Integer i){
theString = '' + i;
}
global StringBuffer(Blob b){
theString = '' + b;
}
global StringBuffer(Boolean b){
theString = '' + b;
}
global StringBuffer(Date d){
theString = '' + d;
}
global StringBuffer(Datetime d){
theString = '' + d;
}
global StringBuffer(ID id){
theString = '' + id;
}
global StringBuffer(Time t){
theString = '' + t;
}
global StringBuffer append(String str){
theString += str; return this;
}
global StringBuffer append(Decimal d){
theString += d; return this;
}
global StringBuffer append(Double d){
theString += d; return this;
}
global StringBuffer append(Long l){
theString += l; return this;
}
global StringBuffer append(Integer i){
theString += i; return this;
}
global StringBuffer append(Blob b){
theString += b; return this;
}
global StringBuffer append(Boolean b){
theString += b; return this;
}
global StringBuffer append(Date d){
theString += d; return this;
}
global StringBuffer append(Datetime d){
theString += d; return this;
}
global StringBuffer append(ID id){
theString += id; return this;
}
global StringBuffer append(Time t){
theString += t; return this;
}
global String toStr(){
return theString;
}
}
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
@IsTest
private class StringBufferTest {
private static testmethod void testDecimal(){
Decimal value = 3.14159;
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testDouble(){
Double value = 3.14159;
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testInteger(){
Integer value = 792392;
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testLong(){
Long value = 792392;
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testID(){
Account foo = new Account(name='test');
insert foo;
Id value = foo.id;
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testBoolean(){
Boolean value = true;
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(!value).toStr(), ''+value+''+value+''+(!value));
}
private static testmethod void testString(){
String value = 'rwkrfkdekf';
System.assertEquals((new StringBuffer()).append(value).toStr(), ''+value);
System.assertEquals((new StringBuffer()).append(value).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testDate(){
Date value = date.newinstance(1960, 2, 17);
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testDatetime(){
Datetime value = datetime.newInstance(2008, 12, 1);
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testTime(){
Time value = Time.newInstance(18, 30, 2, 20);
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
private static testmethod void testBlob(){
Blob value = Blob.valueOf('test123');
System.assertEquals((new StringBuffer(value)).append(value).toStr(), ''+value+''+value);
System.assertEquals((new StringBuffer(value)).append(value).append(value).toStr(), ''+value+''+value+''+value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment