Salesforce Tips: Accessing Person Account fields from Other Object

Once Person Account is activated in Salesforce, you will see a set of additional fields in Person Account record (these fields is the same as the fields in Contact). One issue you will encounter is accessing any of these fields from other related objects. You won’t be able to access this field even with the cross-object formula.

Below is an example of how we can achieve this by writing an Apex Trigger:


Retrieve the Person Account Email from Opportunity

  1. Create a field of Opportunity and label it, ‘Account Email’.
  2. In the Opportunity object, create a trigger.

 

Here is a simple Apex trigger we can use:

 

trigger AccountEmail on Opportunity (before insert, before update) {
    List accountIds = new List();

    for (Opportunity opp : trigger.new){
        accountIds.add(opp.AccountId);
    }

    List oppsAccount = [select Id, IsPersonAccount, PersonEmail from account where Id in: accountIds];
    for (Opportunity opp : trigger.new) {
        for(Account acc : oppsAccount) {
            if(opp.AccountId == acc.Id){
                 if(acc.IsPersonAccount == true) {
                     opp.Account_Email__c = acc.PersonEmail;
                 }
            }
        }
    }
}
Note: If the Email is changed in Account, this trigger will not update the Opportunity’s Account Email unless the Opportunity is edited and save. It would require another trigger to to do that.

Get in touch today to see how WDCi can help your business.