You need to sign in to do that
Don't have an account?
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:
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); } } }
All Answers
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.
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.
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:
Now I just need to write the test class to deploy to production. Thanks again. You're a lifesaver.