1

Anyone know a better way to refactor this code so it won't be hardcoded PersonAccount? Instead, it should be parameter passed in?

String personAccRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();

I have read this blog and with a SOQL query might work but I am still thinking if there is a better approach that won't break? http://smukov.github.io/blog/2018/06/09/Record-Type-Id-By-Developer-Name/

1
  • You could use custom setting (list) or if there are needs for it to be migrated in package use custom metadata and in your apex change such that first you get the value as recordTypeVar from either Custom setting or Custom Metadata and use it
    – Mitesh
    Feb 6 '20 at 13:47
1

Might be one of the few times where it's still better to query the RecordType, as the RecordTypeInfo does not surface IsPersonType, which is more programmatic than checking DeveloperName (especially since you can in fact have multiple).

SELECT DeveloperName FROM RecordType WHERE IsPersonType = true

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.