You need to sign in to do that
Don't have an account?
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:
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); } }
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:
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);
}
}
Thank you both for offering code to resolve the issue. I appreciate it very much. Unfortunately, I am still having the issue.
What is the issue you are getting.
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.