Press enter to see results or esc to cancel.

How to work with data in SFDX?

In the previous blog post we have seen how to work with Salesforce metadata and most of the operations involved. Now we will see how Salesforce cli will help us to perform data related operations. 

 
Lets execute the command sfdx force:data –help which will show us the all possible commands we can use to perform data related activities like insert,update etc… 
 
Below are the operations we can perform with force:data command.
  • force:data:bulk:delete
  • force:data:bulk:status
  • force:data:bulk:upsert
  • force:data:record:create
  • force:data:record:delete
  • force:data:record:get
  • force:data:record:update
  • force:data:soql:query
  • force:data:tree:export
  • force:data:tree:import
 
force:data:bulk

sfdx-bulk-upsert

To upsert the records csv file, external id(I our case I am using the ID field) and SObjectType are the required arguments.
Let’s execute the command to load the Account Data. I have the csv file with all the required fields to load the data.
Execute this command: sfdx force:data:bulk:upsert -s Account -f data/Account1.csv -i Id -w 2
When you are in doubt please check for help be appending the command with –help
At this moment in my org I have 11 Accounts.

Accounts

Once the command execute you can see the results as below.

sfdx-bulk-data-upsert

To know the status please execute the command that was showing in the above picture i.e.

bulk-data-load-status

You can see the count updated in the Account List View. Please check the bulk data loads job also to verify the same.

bulk-data-load-status-ui

Lets use another command to delete all the Accounts created just now. First fetch all the ids.
Execute the command sfdx force:data:bulk:delete –help to know the arguments required to run this command. The Required files are a csv file contains all the ids and the Object of Interest.
Execute the command : sfdx force:data:bulk:delete -s Account -f data/Account_delete.csv

sfdx-data-delete

We can check the same from Bulk data load section.

bulk-data-delete-ui

You can alternatively can query the Account Object/ check in the list view section to confirm the same.
force:data:record
  force:data:record:create  create a record
  force:data:record:delete  delete a record
  force:data:record:get     view a record
  force:data:record:update  update a record
  • Execute the command to create Contact: sfdx force:data:record:create -s Contact -v “LastName=Acme”
  • Execute the command to update Contact: sfdx force:data:record:update  -s Contact -i 0030K00002IDJOW -v “LastName=Acme Update”
  • Execute the command to view Contact: sfdx force:data:record:get -s Contact -i 0030K00002IDJOW
  • Execute the command to delete Contact: sfdx force:data:record:delete -s Contact -i 0030K00002IDJOW
Note: 0030K00002IDJOW  is the contact id created during the first step. I have pasted the output of all the commands below.

Please be noted that these are very basic operations that you can perform on the force:data:record. To check the full capabilities please check the command by appending with –help.
force:data:soql

Execute the command to know what are the required arguments for the force:data:soql:query.

sfdx force:data:soql:query –help , You can see query is the required parameter.
Run this command to know the Count of Ids in Contact
sfdx force:data:soql:query -q “SELECT Count(Id) FROM Contact”

force-data-soql-query

force:data:tree

Execute the command: sfdx force:data:tree:export -q “SELECT Id, Name FROM Contact” -d exportdata

It will generate the Contact.json file in exportdata folder.

force-data-tree-export

 

 Execute the command: sfdx force:data:tree:import -f exportdata/Contact.json

force-data-tree-import

There is so much you can do with the tree commands, where you can plan the data loads in the sequence using plan file. But for now we settle with these simple commands and use cases. We will see the complete understanding of tree commands in another blog post.
I hope you enjoyed reading this blog post. Feel free post your questions/suggestions.