Export Fields’ Metadata of All the objects in Salesforce

Export Fields' Metadata of All the objects in Salesforce into CSV files

Ashish Chauhan | 01-Jun-2023

Recently, I had the opportunity to work on a client project that involved extracting the metadata of all the fields from both standard and custom objects in their Salesforce instance and put into CSV files. However, there was a limitation: I couldn’t install any packages or utilize third-party tools to connect to the Salesforce instance.

This presented a challenge, but it motivated me to explore alternative approaches to fulfill the requirement. After some research, I discovered one possible solution: creating a custom report type to generate an Entity Definition report, which could then be exported to CSV. However, this method required creating a separate report for each object, which seemed inefficient and time-consuming.

Fortunately, I had already set up and connected the Salesforce CLI (sfdx) to the relevant org. This prompted me to leverage its capabilities. I developed a PowerShell script that would extract the definitions of all objects, followed by their respective fields. Subsequently, I stored these field definitions in separate CSV files for each object.

By utilizing the Salesforce CLI and custom scripting, I was able to overcome the limitations and efficiently extract the desired information. Below, you can find the PowerShell script I created for this purpose:

$sObjectList = sfdx force:schema:sobject:list --json | ConvertFrom-Json #You can modify sobject:list to get only the custom object or standard objects.

$sObjListArray = $sObjectList.result #get the list of all sobject and store into a variable

$sObjListArray | ForEach-Object -Process { #this iterate over all the salesforce objects found in the org

  $localVarObj = $_ #copy current object's name into a temporary variable

  $objFldList = sfdx force:schema:sobject:describe -s $localVarObj --json #describe current sobject

  $fieldsInfo = $objFldList | ConvertFrom-Json #convert describe info to PSObject format

  $fieldsOfSobject = $fieldsInfo.result.fields #get the all the fields information of current object.

  $fieldsOfSobject | ForEach-Object -Process {$_ | Export-Csv -NoTypeInformation -Append -Path .\$localVarObj.csv} #export field metadata info of current object into csv file

}

By leveraging the power of the Salesforce CLI and custom scripting, we can successfully fulfill even the most complex metadata extraction requirements. This approach allows us to overcome limitations and efficiently extract the desired information with precision. To exemplify this capability,