Skip to content

Instantly share code, notes, and snippets.

@lucasvalhos
Forked from amphro/sfdx-push-recordtypes
Created May 26, 2020 23:43
Show Gist options
  • Save lucasvalhos/024a4023cf7d58b96a8ce98dad39fc97 to your computer and use it in GitHub Desktop.
Save lucasvalhos/024a4023cf7d58b96a8ce98dad39fc97 to your computer and use it in GitHub Desktop.
Push RecordTypes and Data using SFDX
### Setup Import File and Permission Set
# Step 1. Export the RecordTypes
-> sfdx force:data:tree:export -q "SELECT ID, Name, DeveloperName, SobjectType FROM RecordType" -o data
Wrote 1 records to data/RecordType.json
# Here is what the export might look like
-> more data/RecordType.json
{
"records": [
{
"attributes": {
"type": "RecordType",
"referenceId": "RecordTypeRef1"
},
"Name": "MyType",
"SobjectType": "Account"
}
]
}
# Step 2. Export the Objects
-> sfdx force:data:tree:export -q "SELECT ID, Name FROM Account WHERE RecordType.Name = 'MyType'" -o data/
Wrote 1 records to data/Accounts.json
# Here is what the export looks like.
-> more data/Accounts.json
{
"records": [
{
"attributes": {
"type": "Account",
"referenceId": "AccountRef1"
},
"Name": "Acme"
}
]
}
# Step 3. Put a place mark in the data file.
-> more data/Accounts.json | jq '.records[] |= .+ {"RecordTypeId" : "@RecordTypeRef1"}' | more > data/AccountsWithRecordTypes.json
# Step 4. RecordType.json and AccountsWithRecordTypes.json are files you want to check into source control.
# Step 5. If you don't have a permission set that gives access to your record type, create one and pull the source down, then check into source controll
### Set up a new scratch org - Assumes you have a perm set and data files as described above
# Step 1. Create scratch org
-> sfdx force:org:create -s -f <path to config file>
# Step 2. Import the RecordType - We need this before the perm set push
-> sfdx force:data:tree:import -f data/RecordType.json
# Step 3. Push source (which includes perm set)
-> sfdx force:source:push
# Step 4. Assign permission set
-> sfdx force:user:permset:assign -n <Name-of-your-permset>
# Step 5. Tricky tricky. Add the RecordType reference name to all the accounts.
# a. First test getting the ID
-> sfdx force:data:soql:query -q "SELECT ID FROM RecordType WHERE DeveloperName = 'Test'" --json | jq -r '.records[0].Id'
# b. Replace place mark with ID
-> sed -i -e "s/@RecordTypeRef1/`sfdx force:data:soql:query -q \"SELECT ID FROM RecordType WHERE DeveloperName = 'Test'\" --json | jq -r '.records[0].Id'`/g" data/AccountWithRecordTypes.json
# Here is what the new file looks like
-> more data/AccountWithRecordType.json
{
"records": [
{
"attributes": {
"type": "Account",
"referenceId": "AccountRef1"
},
"Name": "Acme",
"RecordTypeId": "012xx00000022t6AAA"
}
]
}
# Step 6. Now you are ready to import the data
-> sfdx force:data:tree:import -f data/AccountWithRecordType.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment