0

I could notice we can commit Custom object fields and layouts and such, but can we commit the custom object records which we created in org?

2 Answers 2

0

You can use SFDX to retrieve data from your org in CSV or JSON format.

You would use the sf data export tree command.

Check the documentation for what you can do. You can specify file type and file location for example.

0

Custom Object definitions and Custom Field definitions are metadata.
Records are data. If you want to have a set of data on your repository you have to export it from your org (i.e. via Data Loader or SFDX CLI), then commit the files (usually a csv).

Keep in mind that if those records have relationship fields (lookup or master-detail), the Ids you're going to export are valid just in the source org, so if you need to import them on other orgs, you have to export their parent records too and handle the relationship issues.

Luckly there are several tools that handle such issues automatically, like SFDX Data Move Utility (SFDMU), a plugin for SFDX CLI. To install it run the following command from your terminal (the one built-in in VS Code is fine):

sfdx plugins:install sfdmu

Let's say you want to export Account and Contact keeping the relationship, you could create a AccountWithContact folder in your repository and in that folder a export.json file.
In this file you must write a SOQL for each object (case sensitive, so double-check every SOQL) you want to export. Assuming that each Account record has unique Name value and each Contact record has unique Email value, it would look like:

{
    "allOrNone": true,
    "objects": [
        {
            "query": "SELECT Id, Name, Industry FROM Account",
            "operation": "Upsert",
            "externalId": "Name"
        },
        {
            "query": "SELECT Id, FirstName, LastName, Email, AccountId FROM Contact",
            "operation": "Upsert",
            "externalId": "Email"
        }
    ]
}

The query can include WHERE, ORDER BY, LIMIT clause.

Now you should go to the directory where your export.json is located. Assuming that you created the AccountWithContact folder under the root of your SFDX project, just run cd AccountWithContact.
Export records to csv files:

sfdx sfdmu:run --sourceusername ORG_ALIAS_OR_USERNAME --targetusername csvfile

Import records from csv files:

sfdx sfdmu:run --sourceusername csvfile --targetusername ORG_ALIAS_OR_USERNAME
1
  • 1
    thanks @RubenDG
    – SF Learner
    Jul 27, 2023 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .