Salesforce export metadata to csv

Exporting salesforce metadata to csv, iterate through SObject and fields and show Label, API Name, FLS, Custom/Standard field.

package com.app.salesforce.metadata;

import com.app.salesforce.file.FileService;
import com.sforce.soap.partner.*;
import com.sforce.ws.ConnectionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * Created by thysmichels on 11/7/16.
 */

@Service
public class MetadataService {

   @Autowired
   PartnerConnection partnerConnection;

    @Autowired
    FileService fileService;

   public void exportSObjectMetadata() throws ConnectionException, IOException {
       DescribeGlobalResult describeMetadataResult = partnerConnection.describeGlobal();
       DescribeGlobalSObjectResult[] sObjectResults = describeMetadataResult.getSobjects();
       String csvFile = "export/sobjectMetadata.csv";
       FileWriter writer = new FileWriter(csvFile);
       fileService.writeLine(writer, Arrays.asList("Label", "API Name", "Custom", "Label", "API Name", "Custom", "FLS"));
       for (DescribeGlobalSObjectResult sObjectResult : sObjectResults){
           if (sObjectResult.isCreateable() && sObjectResult.isQueryable()){
               DescribeSObjectResult describeSObjectResult = partnerConnection.describeSObject(sObjectResult.getName());
               for (Field field : describeSObjectResult.getFields()){
                   fileService.writeLine(writer, Arrays.asList(sObjectResult.getLabel(), sObjectResult.getName(), String.valueOf(sObjectResult.isCustom()), field.getLabel(),field.getName(), String.valueOf(field.isCustom()), String.valueOf(field.isPermissionable())));
               }
           }
       }

       writer.flush();
       writer.close();

   }


    public void updateCsvSObjectMetadata() throws ConnectionException, IOException {
        String csvFile = "import/sobjectFLS.csv";
        Map<String, DescribeGlobalSObjectResult> mapOfGlobalDescibeSObjectResult = new HashMap<>();
        Map<String, Map<String, MetadataModel>> metadataModelMapMap = new HashMap<>();

        DescribeGlobalResult describeMetadataResult = partnerConnection.describeGlobal();

        DescribeGlobalSObjectResult[] sObjectResults = describeMetadataResult.getSobjects();
        for (DescribeGlobalSObjectResult sObjectResult : sObjectResults) {
            if (sObjectResult.isCreateable()) {
                mapOfGlobalDescibeSObjectResult.put(sObjectResult.getLabel(), sObjectResult);
            }
        }

   
        for (DescribeGlobalSObjectResult sObjectResult : sObjectResults){
            if (sObjectResult.isCreateable() && sObjectResult.isQueryable()) {

                DescribeSObjectResult describeSObjectResult = partnerConnection.describeSObject(sObjectResult.getName());
                Map<String, MetadataModel> metadataModelMap = new HashMap<>();
                for (Field field : describeSObjectResult.getFields()) {
                    MetadataModel metadataModel = new MetadataModel();
                    metadataModel.setObjectLabel(sObjectResult.getLabel());
                    metadataModel.setObjectApi(sObjectResult.getName());
                    metadataModel.setObjectIsCustom(sObjectResult.isCustom());
                    metadataModel.setFieldLabel(field.getLabel());
                    metadataModel.setFieldApi(field.getName());
                    metadataModel.setFieldIsCustom(field.isCustom());
                    metadataModel.setIsFLS(field.isPermissionable());
                    metadataModelMap.put(field.getLabel(), metadataModel);
                }
                metadataModelMapMap.put(sObjectResult.getLabel(), metadataModelMap);

            }
        }

        String csvFileExport = "export/sobjectFLSCopy.csv";
        FileWriter writer = new FileWriter(csvFileExport);
        fileService.writeLine(writer, Arrays.asList("Object Label", "Object API Name", "Custom object (Y/N)", "Field Label", "Field API Name", "Custom Field (Y/N)", "Is Field Settable (Y/N) done 11/6","Used / System?","Encrypted","Administrator","Client Services","Community User","Developer","Executive","Marketing","Product Management","Production Support","Integration","System Administrator Clone","Client Service Manager","View Encrypted Data","Goals","Financial Accounts","Financial Planning","Portfolio Management","Case Management","Goals - Read Write","Financial Accounts - Read Write","Financial Planning - Read Write","Portfolio Management - Read Write","Case Management - Read Write"));

        Scanner scanner = new Scanner(new File(csvFile));
        scanner.nextLine();
        while (scanner.hasNext()) {

            List<String> line = fileService.parseLine(scanner.nextLine());
            
                if (metadataModelMapMap.get(line.get(0))!=null && metadataModelMapMap.get(line.get(0)).get(line.get(3))!=null){
                    fileService.writeLine(writer, Arrays.asList(line.get(0), metadataModelMapMap.get(line.get(0)).get(line.get(3)).getObjectApi(), metadataModelMapMap.get(line.get(0)).get(line.get(3)).getObjectIsCustom().toString(), line.get(3), metadataModelMapMap.get(line.get(0)).get(line.get(3)).getFieldApi(), metadataModelMapMap.get(line.get(0)).get(line.get(3)).getFieldIsCustom().toString(), metadataModelMapMap.get(line.get(0)).get(line.get(3)).getIsFLS().toString(), line.get(7),line.get(8),line.get(9),line.get(10),line.get(11),line.get(12),line.get(13),line.get(14),line.get(15),line.get(16),line.get(17),line.get(18),line.get(19),line.get(20),line.get(21),line.get(22),line.get(23),line.get(24),line.get(25),line.get(26),line.get(27),line.get(28),line.get(29),line.get(30)));
                } else {
                    System.out.println(line.get(0) +  "--> " + line.get(3));
                }
        }
        scanner.close();

        writer.flush();
        writer.close();
    }
}

Output

Case	Case	false	Case ID	Id	false	false
Case	Case	false	Deleted	IsDeleted	false	false
Case	Case	false	Case Number	CaseNumber	false	false
Case	Case	false	Client Name	ContactId	false	true
Case	Case	false	Household	AccountId	false	true
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: