Monday, June 29, 2020

Export Salesforce Object field names into CSV file

Sample Code
Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get('Account').getDescribe().Fields.getMap();
list<string> selectFields = new list<string>();
string excelHeader = 'ApexName\n';
if (fMap != null){
    for (Schema.SObjectField ft : fMap.values()){
        Schema.DescribeFieldResult fd = ft.getDescribe();
        excelHeader += fd.getName() +'\n';
    }
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(excelHeader);
string csvname= 'AccountFields.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'Your Email'};
String subject = 'Account fields CSV';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody('Account Fields');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Output



No comments:

Post a Comment