• marc.lucoff
  • NEWBIE
  • 10 Points
  • Member since 2017
  • Salesforce Administrator

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 7
    Replies
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);
        }
    }
}

 
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);
    }
}

 
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);
        }
    }
}

 
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);
    }
}