Find your content:

Search form

You are here

Dynamically determining if PersonAccounts exist in APEX

 
Share

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.


Attribution to: Michael Welburn

Possible Suggestion/Solution #1

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);

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

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

try{
    acc.put('personassistantname','test')
    return true;
}
catch(Exception ex){return false;}

Attribution to: Greg Grinberg
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2262

My Block Status

My Block Content