2

Using the RecordTypeInfo class allows us to get information about the Record Types and which one is set to default for the running user's profile.

The (Person) Account is however a special case, because it is can have 2 defaults set for a profile: one in the same way as other objects (see top section of image 1) but also a specific default record type for both a regular Account as well as a Person Account.

image 1

With the following piece of code we can retrieve the default Record Type for the running user and also output the results in the debug log:

Map<Id, Schema.RecordTypeInfo> recordTypeInfoMap = Schema.Account.getSObjectType().getDescribe().getRecordTypeInfosById();
Id defaultRecordtypeId;

for (RecordTypeInfo recordTypeInfo : recordTypeInfoMap.values()) {
    System.debug(recordTypeInfo.getName() + ': ' + recordTypeInfo.isDefaultRecordTypeMapping());
    if (recordTypeInfo.isDefaultRecordTypeMapping()) {
        defaultRecordtypeId  = recordTypeInfo.getRecordTypeId();
    }
}

The result of this (based on the configuration shown in the image) would be a list like below:

[4]|DEBUG|Commercial Account: true
[4]|DEBUG|Domestic Account: false
... any other record type you have
[4]|DEBUG|Master: false

So using the isDefaultRecordTypeMapping() method from the RecordTypeInfo class only retrieves the default Account Record Type that is set for Account in general, regardless of whether it is a Person Account or not.

Question: Is there a way to also retrieve from the Schema that Domestic Account is set as the Person Account Default Record Type?

1 Answer 1

0

We ended up (re)using a Custom Setting to store the default Commercial and Person Account record types, see the code below.

When the sObject type of the record is Account, we use the 2 custom settings, if it is any other sObject type, we use the Record Type Describe to return the default record type for that object.

private static Id getDefaultRecordTypeId(SObject record) {
    Map<Id, Schema.RecordTypeInfo> recordTypeInfoMap = record.getSObjectType().getDescribe().getRecordTypeInfosById();

    // Account has 2 defaults, 1 for commercial and 1 for domestic, but only one can be retrieved as default by the schema describe so we do a custom settings trick here
    if (record.getSObjectType() == Schema.Account.getSObjectType()) {
        RecordTypeIds__c recordTypeIds = RecordTypeIds__c.getInstance();
        if ((Boolean) record.get('IsPersonAccount')) {
            return recordTypeIds.AccDomesticAccount18d__c;
        } else {
            return recordTypeIds.AccCommercialAccount18d__c;
        }
    }

    // All other objects have just 1 default record type, so we can simply return that one.
    for (RecordTypeInfo recordTypeInfo : recordTypeInfoMap.values()) {
        if (recordTypeInfo.isDefaultRecordTypeMapping()) {
            return recordTypeInfo.getRecordTypeId();
        }
    }
    return null;
}

PS. This code assumes that when the record is an account, the IsPrivateAccount field is also present, as there does not seem to be a way to use Describe information to determine whether it is a Person Account or not.

You must log in to answer this question.

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