Adminstrator · Apex · Salesforce

How to export Salesforce Object field name into CSV file?

Please run below script through developer console you will receive a email with list of field for that object.

Note: Please replace with your email address and object based on your need.

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= ‘ApexClassBatchName.csv’;
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {‘Enter your email address’};
String subject =’Apex Batch Class Name CSV’;
email.setToAddresses( toAddresses );
email.setPlainTextBody(‘Apex Batch Class Name CSV ‘);
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s