1

I recently inherited a large project with lots of Apex code. I'm trying to use Salesforce DX to develop new features, but am unable to push code to a scratch org, because when I run sfdx force:org:push, the system tells me about a bunch of errors in the codebase. Many of the errors are Field does not exist: RecordTypeId on <whatever> when creating new objects. Here's an example from a test method:

Account a = new Account(...etc...);
Contact c = new Contact(RecordTypeId = GlobalVariables.myContactTypeId,
AccountId = account.Id, FirstName = 'First', LastName = 'Student');
insert student;

These lines produces the error: Field does not exist: RecordTypeId on Contact (36:21)

I believe this test runs as part of our standard suite without fail on production, so why is this flagged as an error here? This unit test is in a file marked with API 19.0, so it's quite old, but I thought scratch orgs accounted for different API versions.

Thanks!

  • 5
    In order for that field to resolve you must have Recordtypes enabled on the contact object. Can you configure the org shape to include record types via SFDX? No sure since I have not played with it as it all seems way to complicated to get into right now. – Eric Aug 22 '17 at 18:39
  • 2
    You need to configure at least one RecordType on the Contact object. – Adrian Larson Aug 22 '17 at 18:39
5

You can put your objects into your source control (which you should, and then deploy them along with all the code. Your record types will also be included as part of the deployment. As long as the record types are deployed alongside the code, there shouldn't be any compilation errors.

  • What I ended up with is adding the record types to package.xml. – incidentist Nov 20 '17 at 18:32
0

Here's what I ended up with. Thanks to Adrian Larson's comment, I researched how to export data using sfdx. I found that on Trailhead: https://trailhead.salesforce.com/modules/sfdx_app_dev/units/sfdx_app_dev_create_app#Tdxn4tBK-heading6

In the end, this is the command I ran to export:

sfdx force:data:tree:export -q "SELECT Id, BusinessProcessId, Description, DeveloperName, Name, NamespacePrefix, SobjectType FROM RecordType WHERE SobjectType IN ('Account', 'Contact', 'Campaign', 'CampaignMember', 'Case', 'Contact', 'Contract', 'Lead', 'QuickText', 'Solution')" -d ./data -u Prod

Notes:

  • The first issue I ran into is that you need to filter out custom object types. You can't do the initial metadata push until RecordTypes are in place, but you can't import custom object record types, because the custom objects they refer to haven't been defined in the scratch org yet. So I'll do a second import for custom object record types after the metadata is in place.
  • I had to remove the IsActive field from the query, because it threw a permission error on import. Not sure what the deal is there -- the query works fine in the Developer Console.
  • I had to filter out Opportunity record types. The export query does not return the BusinessProcessId field, even though (again) the same query works in the developer console in the developer console. The BusinessProcessId field is required for Opportunity record types, so the import fails with Field BusinessProcessId is required for record types on Opportunity.

This was enough to get me going with my initial metadata push, though there are too many loose ends for this to be a long-term solution.

0

We regularly migrate projects from the classic 'org centric' structure to SFDX. There is a great guide on migrating https://developer.salesforce.com/blogs/developer-relations/2017/07/migrating-existing-projects-salesforce-dx.html. BUT...that guide doesn't take into account record types.

Here is what we do:

  • We setup the project
  • Download and convert the code to sfdx format
  • Create a scratch org
  • When we have record types on an object, we login into the scratch org and manually create the same object (same API name) and create a bogus record type
  • Finally we push: sfdx force:source:push --forceoverwrite

I am looking into ways to do this 'automagically' via Chrome headless or something because this is a pain for CI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.