4

Is there a Salesforce provided method/process to Programmatically distinguish between PersonAccount and Business Account fields ?

I am using the getSObjectType().getDescribe().fields to get fields of Account where result includes PersonAccount and Business type Account Fields as well.

Is this a right way to identify Person Account fields using the condition as in the code snippet below. Am I missing anything here?

Map<String,Schema.SObjectField> fieldMap = Schema.Account.getSObjectType().getDescribe().fields.getMap();
for (String f : fieldMap.keySet()){
      DescribeFieldResult fieldDescribe = fieldMap.get(f).getDescribe();
      if ((f.startsWith('person') && !fieldDescribe.isCustom()) || f.endsWith('__pc') || fieldDescribe.isNameField())
          System.debug( f );
}

Similar Question (Old) There exist a very old post with same question, which does not have a solution. Hence I created this Question.

Thanks for your time.

2
  • 2
    In the question you referenced, one of the answers mentions that Person account fields end in __pc, so you should be able to use getName() on the DescribeFieldResult and go from there. Feb 10, 2018 at 0:27
  • @DavidCheng Yes thats one missing part ( fields ending with __pc are just custom fields), Custom fields added to Contact object are considered as Person Account Custom Fields which ends with __pc. And Standard fields starts with "person" with no suffix. Guess we covered all possible cases of Person account fields, Please correct me if I am wrong. (1) >> Name fields (excluding 'Name') . . . . (2) >> Standard Person Account Fields (fieldName starts with "person" & is Not a Custom field) . . . . (3) >> Custom Person Account Fields (fieldName ends with __pc)
    – Sriram
    Feb 10, 2018 at 2:06

0

You must log in to answer this question.

Browse other questions tagged .