Possible Duplicate:
What’s the best way to check if person accounts are enabled via Apex Code?

I am wondering if there is a way to determine if PersonAccounts have been turned on in the organization via a query (or some other way). My scenario is that I wrote some code that goes against contacts, and tried to make it generic enough that if PersonAccounts are turned on that it leverages the information stored on that record instead, but I couldn't determine the best (or rather, a working) way to set up logic to branch into the two paths. Is this possible?

UPDATE: To be a bit more specific, I am looking to write a package that does A if PersonAccounts are enabled, and does B if they are not. Logically everything that happens after that is about the same, its just a matter of whether I'm leveraging the PersonAccount or the Contact. From what I can tell, if PersonAccounts are not enabled in the org, you can't even reference that object in APEX code.


2 Answers 2


If person Account is enabled we have a boolean field called isperson Account in Account object.

SO you may need a describe sobject call and check if Account object has isperson Account field then process the logic .

So a schema describe call will be helpful and checking if isperson Account field exists is helpful

//To get a map of all fields an object
 Map<String, Schema.SObjectField> M =Schema.SObjectType.Account.fields.getMap();
//Boolean flag to detect person account enabled or not
Boolean isPersonAccountEnabled=M.containsKey('IsPersonAccount');
//Debug Statements
 System.debug('MAP DEBUG'+M);
 System.debug('PERSON ACCOUNT ENABLED'+isPersonAccountEnabled);
  • 1
    A code example would also be helpful.
    – metadaddy
    Oct 12, 2012 at 15:13
  • Sure @metadaddy will have a code on that post once i am free.Presently i gave rough approach .Thanks nice suggestion to put code also Oct 12, 2012 at 15:17
  • @metadaddy updated the answer with code snippet Oct 13, 2012 at 8:38

The describe method above definitely works but if you don't want to spend a describe call you can test if the new PersonAccount fields exist by trying to set a field that only exists on person accounts dynamically and catching the exception if it doesn't exist

    return true;
catch(Exception ex){return false;}
  • Just on programming best practices I think that is a bad idea; one should not purposely use exceptions for this kind of flow control.
    – Mike Chale
    Oct 12, 2012 at 17:01
  • On another question about this @E.J. Wilburn demonstrated a considerable performance advantage by using this exception based approach over a describe-based approach: salesforce.stackexchange.com/a/1056/60 Is the exception path uglier? Undoubtedly, but it also has merits. Oct 12, 2012 at 18:12
  • Doh...didn't remember to check for duplicates. Should I delete my answer here? E.J. Wilburn's answer is obviously the best one and if there are no answers on this question and a comment that it is a duplicate people will go to that question. Oct 12, 2012 at 18:32
  • @grigriforce That was me not E.J Wilburn's. Oct 12, 2012 at 18:57
  • 1
    I'm surprised that the exception route would be faster; just goes to show that benchmarking is better than a generalization sometimes. Thanks, @ca_peterson.
    – Mike Chale
    Oct 13, 2012 at 13:02

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