Salesforce : Determine if Person Account is enabled with Apex

When you are developing an App in Salesforce, you may need to have separate sets of logic if Person Account is enabled in the Salesforce Org. How do you know if Person Account is enabled in an org? When Person Account is enabled, a new field (IsPersonAccount) will be added in the Account object. You can check it with the following sample:

 

Map<string, Schema.SObjectField> accountFieldsMap = Schema.sObjectType.Account.fields.getMap();

if( accountFieldsMap.get('IsPersonAccount') == null) {
// Person Account is enabled in this org
} else {
// Person Account is not enabled in this org
}