Importing and Exporting Records Between Environments in Salesforce DX
One of the drawbacks of a new development environment in Salesforce is that it does not contain the data on which our code may rely. For example, in our production environment, we may have an object named Status with a record named New. Additionally, we have an object in the development environment named Task with a lookup field named Status pointing to the Status object. Lastly, we have a validation rule on the Task object that enforces logic that a Task record can only be opened if the value in the Status field equals New. When we create a new development environment, the New record will not be imported, and if we try to save a new Task, we will receive an error message from the validation rule. A simple solution to this problem is to import the New record from our production environment to the development environment.
Exporting Records
To export the required record from our production environment, follow these steps:
- Open your Salesforce DX project.
- Open the terminal window by pressing Ctrl + ~.
- Enter the following code:
sfdx force:data:tree:export -q "SELECT Id, Name FROM Status__c WHERE Name ='New'" -d ./exports -u Prod
- sfdx - Salesforce CLI command prefix.
- force:data:tree:export - Command to export data from Salesforce.
- -q "SELECT Id, Name FROM Status__c WHERE Name = 'New'" - Flag for specifying the query and the query itself to retrieve the required records.
- -d ./exports - Flag to specify the destination for saving the query results.
- -u Prod - Flag for choosing the user or their alias for the production environment. In this example, I have defined that the alias for my user in the production environment is Prod. Yours could be a different value or the user's name in email format.
Now, if we look at the directory structure in our project, alongside the "config" and "force-app" directories, there is a new directory called "exports." Inside the "exports" directory, you will find a file named "Status__c.json" containing the data of the records we exported from the production environment. This file will be used to import the record we exported from the production environment to our development environment.
Importing Records
To import the required record into our development environment, follow these steps:
- Enter the following code in the terminal:
sfdx force:data:tree:import -f exports/Status__c -u Dev
- force:data:tree:import - Command to import data into Salesforce.
- -f export/Status__c - Flag and path to the file from which we will import the data into our development environment.
- -u Dev - Flag for choosing the user or their alias for the development environment. In this example, I have defined that the alias for my user in the development environment is Dev. Yours could be a different value or the user's name in email format.
- Upon running the command, the terminal will display a success message with the data of the record that has been imported into the development environment.