function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
marc.lucoffmarc.lucoff 

How do I auto-populate a custom lookup field on a person account based on the value of another custom field?

I asked this question a little over a week ago: We have person accounts enabled in our org. We have business accounts with a custom picklist field called Business Account Type with the values "Headquarters" and "Branch Office". If "Branch Office" is selected, a validation rule triggers on save that will require a value in a custom text field called Branch ID (Branch_ID__c). On person account is the same Branch ID field and if it is populated with a value that matches the Branch ID on a business account, a custom lookup field to account called Company (Company__c) should auto-poulate with the account name on the person account.

I have tried using the process automation tools; flow and process builder but was unable to get them to work. I think an apex trigger is required to resolve this issue. I have even tried to write the trigger myself but I have very little experience with triggers. Here is what I have so far which I don't think is anything close to what I need:
 
trigger updateCompanyLookup on Account (before insert) {
   
    Set<String> accountIdList = new Set<String>();
    Map<String,String> accNameMap = new Map<String,String>();

    for (Account pa : Trigger.new) {
        accountIdList.add(pa.Branch_ID__c);
    }
    
    for(Account ba : [SELECT Id, Branch_ID__c FROM Account WHERE Branch_ID__c IN  :accountIdList AND IsPersonAccount=True]) {
        if(ba.Branch_ID__c!=null){
                  accNameMap.put(ba.Branch_ID__c,ba.Id);
        }
  
    }
    
    for(Account pa : Trigger.new) {
        if(accNameMap.containsKey(pa.Branch_ID__c)){
            pa.Id = accNameMap.get(pa.Company__c);
        }
    }
}

 
Best Answer chosen by marc.lucoff
Briana L.Briana L.
trigger updateCompanyLookup on Account (before insert) {
   
	//Get a set of all the Branch IDs related to the Person Accounts in this insert
    Set<String> BranchIDstoFind = new Set<String>();
    for (Account acc : Trigger.new) {
       //If the newly inserted account is a Person account with a Branch ID
	   if(acc.isPersonAccount==true && string.isNotBlank(acc.Branch_ID__c)){ 			BranchIDstoFind.add(acc.Branch_ID__c);
	   }
    }
	
	//Declare a map of the branch ID to the Business Account ID
	Map<String,ID> mapBranchIDtoBAccID = new Map<String,ID>();
	
	//SOQL the "Business Accounts" for the ones that match the Branch IDS
	List<Account> businessAccounts = [SELECT Id, Branch_ID__c FROM Account WHERE Branch_ID__c IN  :BranchIDstoFind AND IsPersonAccount=False];
	if(businessAccounts.size()>0){
		for(Account ba : businessAccounts){
			mapBranchIDtoBAccID.put(ba.Branch_ID__c, ba.ID);
		}
	}
	
	//Now, loop over the newly inserted Accounts
    for(Account acc : Trigger.new) {
		//if the account is a person account and we have a map of matching Business accounts
		if(acc.isPersonAccount==true && mapBranchIDtoBAccID !=null && !mapBranchIDtoBAccID.isEmpty()){
			//if there is a branch ID on this person account
			if(String.isNotBlank(acc.Branch_ID__c)){
				//Get the business account from the map and set the lookup
				ID businessAccountID = mapBranchIDtoBAccID.get(acc.Branch_ID__c);
				if(businessAccountID!=null){
					acc.Company__c = businessAccountID;
				}				
			}
		}

    }
}

 

All Answers

Briana L.Briana L.
Can you please clarify your requirements? I think this is what you are trying to achieve:

Requirements:
If a Person Account is inserted and has a value for "Branch ID" you must search for the Business Account with the matching "Branch ID"
If a matching "Branch ID" is found on a Business Account, set the lookup field on the Person Account to lookup to the Business Account.
 
marc.lucoffmarc.lucoff
Hi @Briana L.
That is absolutely correct. Our business use case is the following: Person accounts are created from one of two ways 1) a converted lead; or 2) created on bulk import based on record type. Business accounts are created independantly with either a headquarter ID or a Branch ID. If a person account has a Branch ID, perform a search and find the business account with the matching Branch ID and populate the person account's Company lookup field with that matching business account name.
Briana L.Briana L.
trigger updateCompanyLookup on Account (before insert) {
   
	//Get a set of all the Branch IDs related to the Person Accounts in this insert
    Set<String> BranchIDstoFind = new Set<String>();
    for (Account acc : Trigger.new) {
       //If the newly inserted account is a Person account with a Branch ID
	   if(acc.isPersonAccount==true && string.isNotBlank(acc.Branch_ID__c)){ 			BranchIDstoFind.add(acc.Branch_ID__c);
	   }
    }
	
	//Declare a map of the branch ID to the Business Account ID
	Map<String,ID> mapBranchIDtoBAccID = new Map<String,ID>();
	
	//SOQL the "Business Accounts" for the ones that match the Branch IDS
	List<Account> businessAccounts = [SELECT Id, Branch_ID__c FROM Account WHERE Branch_ID__c IN  :BranchIDstoFind AND IsPersonAccount=False];
	if(businessAccounts.size()>0){
		for(Account ba : businessAccounts){
			mapBranchIDtoBAccID.put(ba.Branch_ID__c, ba.ID);
		}
	}
	
	//Now, loop over the newly inserted Accounts
    for(Account acc : Trigger.new) {
		//if the account is a person account and we have a map of matching Business accounts
		if(acc.isPersonAccount==true && mapBranchIDtoBAccID !=null && !mapBranchIDtoBAccID.isEmpty()){
			//if there is a branch ID on this person account
			if(String.isNotBlank(acc.Branch_ID__c)){
				//Get the business account from the map and set the lookup
				ID businessAccountID = mapBranchIDtoBAccID.get(acc.Branch_ID__c);
				if(businessAccountID!=null){
					acc.Company__c = businessAccountID;
				}				
			}
		}

    }
}

 
This was selected as the best answer
marc.lucoffmarc.lucoff
Hi @Briana L.

Thank you so very much for your help. I am beyond grateful. Your trigger worked perfectly but I did have to add a "before insert" to the first line of the trigger. Here's the updated first line:
 
trigger updateCompanyLookup on Account (before insert, before update) {

Now I just need to write the test class to deploy to production. Thanks again. You're a lifesaver. 
marc.lucoffmarc.lucoff
sorry... i added the "before update"