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 

I am trying to auto-populate a custom lookup field on a person account based on the value of another custom field.

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" or "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, a custom lookup field to account called Company (Company__c) should auto-poulate with the account name.

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 (after insert) {
    List<Id> accountIdList = new List<Id>();
    Map<Id,Id> accNameMap = new Map<Id,Id>();
    for (Account pa : Trigger.new) {
        accountIdList.add(pa.Id);
    }
    for(Account ba : [SELECT Branch_ID__c FROM Account WHERE id in :accountIdList]) {
        accNameMap.put(ba.Id,ba.Branch_ID__c);
    }
    
    for(Account pa : Trigger.new) {
        if(accNameMap.containsKey(pa.Id))
            pa.Company__c = accNameMap.get(pa.Id);
    }
}

 
Raj VakatiRaj Vakati
Can you try this code? Change API Names..  
 
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(Company__c ba : [SELECT Id, Branch_ID__c,Name FROM Company__c  where Branch_ID__c in :accountIdList ]) {
        accNameMap.put(ba.Branch_ID__c,ba.Id);
    }
    
    for(Account pa : Trigger.new) {
        if(accNameMap.containsKey(pa.Branch_ID__c))
            pa.Company__c = accNameMap.get(pa.Branch_ID__c);
    }
}

 
marc.lucoffmarc.lucoff
Hi @Raj V,

Thank you for responding and providing the code. Company__c is a custom field on Account and not a custom object so I revised your code accordingly. It's still not populating the Comapny__c field. Here's what I used:
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]) {
        accNameMap.put(ba.Branch_ID__c,ba.Id);
    }
    
    for(Account pa : Trigger.new) {
        if(accNameMap.containsKey(pa.Branch_ID__c))
            pa.Company__c = accNameMap.get(pa.Branch_ID__c);
    }
}

 
Raj VakatiRaj Vakati
Company__c  is text filed or lookup field? If its lookup field can you give me SOQL query to get the data  
marc.lucoffmarc.lucoff
Company__c is a lookup field on account. Here's a screenshot:

User-added image
Raj VakatiRaj Vakati
Try below code
 
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.Company__c = accNameMap.get(pa.Branch_ID__c);
    }
}


 
Raj VakatiRaj Vakati
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  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.Company__c = accNameMap.get(pa.Branch_ID__c);
		}
    }
}

 
niven sfniven sf
trigger updateCompanyLookup on Account (before insert,before update) {
    set<String> accountIds = new set<String>();
   
    for (Account pa : Trigger.new) {
        if(pa.Branch_ID__c != null)
             accountIds.add(pa.Branch_ID__c);
    }
  Map<String,Id> accountMap = new Map<String,Id>();
    for(Account ba : [SELECT Id, Branch_ID__c FROM Account WHERE Branch_ID__c  in :accountIdList]) {
        accountMap.put(ba.Branch_ID__c,ba.id);
    }
    
    for(Account pa : Trigger.new) {
            pa.Company__c = accountMap.get(pa.Id);
    }
}
marc.lucoffmarc.lucoff
To Raj V and nlven sf,

Thank you both for offering code to resolve the issue. I appreciate it very much. Unfortunately, I am still having the issue.
niven sfniven sf
hello marc.lucoff,
   
   What is the issue you are getting.
marc.lucoffmarc.lucoff
hello nlven sf

My specific issue is not having the trigger auto-populate the Company custom lookup field on the person account when the Branch ID field has a value in it. The Company field is a lookup to account. On a business account, there is also a Branch ID field. As long as that field has a value in it and the person account Branch ID field has the same value, the Company field will fill with the business account name with the matching Branch ID.