Activity Forums Salesforce® Discussions Best way to check if person accounts are enabled via Salesforce Apex Code?

  • PRANAV

    Member
    August 26, 2016 at 10:52 am

    Hi Tanu,

    Follow these steps:

    • Try to access the isPersonAccount property on an Account and catch any exception that occurs if that property is missing.
    • If an exception is generated then person accounts are disabled. Otherwise they're enabled.
    • To avoid making person accounts required for the package you can assign the Account object to an sObject and use sObject.get( 'isPersonAccount' ) rather than accessing that property directly on the Account object.

    Hope this helps you.

  • Archit

    Member
    January 15, 2018 at 2:39 pm

    public class TestPersonAccount {
    public static Boolean personAccountsEnabled()
    {
    try
    {

    sObject testObject = new Account();
    testObject.get( 'isPersonAccount' );
    System.debug('Account Enabled');
    return true;
    }
    catch( Exception ex )
    {
    System.debug('Account Disable');
    return false;
    }
    }

    }

    Please try above code, hope it would be helpful!!

    Thanks!!

  • PRANAV

    Member
    January 15, 2018 at 3:07 pm

    Hi

    I find an interesting way to check Person Account is enabled or not

    boolean abc = Schema.sObjectType.Account.fields.getMap().containsKey( ‘isPersonAccount’ );
    system.debug(‘@@@’+abc);

    Hope this will minimize your code and helps you.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos