Monday, January 27, 2014

What is the best way to check whether organization have PersonAccount enable or not using Apex

What is the best way to check whether organization have PersonAccount enable or not using Apex

// Test to see if person accounts are enabled.
public Boolean personAccountsEnabled()
{
    try
    {
        // Try to use the isPersonAccount field.
        sObject testObject = new Account();
        testObject.get( 'isPersonAccount' );
        // If we got here without an exception, return true.
        return true;
    }
    catch( Exception ex )
    {
        // An exception was generated trying to access the isPersonAccount field
        // so person accounts aren't enabled; return false.
        return false;
    }
}

Method 2:
// Check to see if person accounts are enabled.
public Boolean personAccountsEnabled()
{
    // Describe the Account object to get a map of all fields
    // then check to see if the map contains the field 'isPersonAccount'
    return Schema.sObjectType.Account.fields.getMap().containsKey( 'isPersonAccount' );
}


No comments:

Post a Comment