1

Is the code below ready to handle bulk changes? Is there a limit to how many Accounts I can be bulk updating with this trigger active?

Trigger:

trigger transferRelationshipTeamFromAcctOnAcctUpdate on Account (after update) {
    AccountManager.handleUpdateTransferRelationshipTeamFromAcctOnAcctUpdate(trigger.new);
}

Class:

public class AccountManager {
public static void handleUpdateTransferRelationshipTeamFromAcctOnAcctUpdate(List<Account> AcctsTrNew){
    //Query a list of all Financial Accounts where the Client is in Trigger.New
    List<Financial_Account__c> finAccts = [
        SELECT Id, Client__c, Wealth_Advisor_Lookup__c, Portfolio_Manager_Lookup__c, Trust_Officer_Lookup__c,
               Other_Team_Member_1_Lookup__c, Other_Team_Member_2_Lookup__c
        FROM Financial_Account__c
        WHERE Client__c IN : AcctsTrNew];

    //Create a list to hold Financial Accounts to Update
    List<Financial_Account__c> finAcctsToUpdate = new List<Financial_Account__c>();

    //Loop through all Accounts in Trigger.New
    for(Account acct : AcctsTrNew){
        //Loop through all Financial Accounts whose Client is in Trigger.New
        for(Financial_Account__c finAcct : finAccts){
            //Check to see if the active Financial Account's is the active Account
            if(acct.Id == finAcct.client__c){
                //If yes, set the Financial Account's Relationship team to = the Account's Relationship Team
                finAcct.Wealth_Advisor_Lookup__c = acct.Wealth_Advisor__c;
                finAcct.Portfolio_Manager_Lookup__c = acct.Portfolio_Manager_Lookup__c;
                finAcct.Trust_Officer_Lookup__c = acct.Trust_Officer__c;
                finAcct.Other_Team_Member_1_Lookup__c = acct.Other_Team_Member_1__c;
                finAcct.Other_Team_Member_2_Lookup__c = acct.Other_Team_Member_2__c;

                //Add the Financial Account to the list of Financial Accounts to Update
                finAcctsToUpdate.add(finAcct);
            }
        }
    }
    //Update the Financial Accounts
    update finAcctsToUpdate;
}
}

2 Answers 2

2

The only optimization i find missing is where I'd recommended to use a Map, rather than a list, so that you can retrieve the value of the Client with a direct lookup, rather than nested for loops - sheerly results in a lot less script statements executed.

So,

 public class AccountManager {

/pass in the Trigger.NewMap()
public static void handleUpdateTransferRelationshipTeamFromAcctOnAcctUpdate(Map<Id,Account> TriggerNewMap{
    //Query a list of all Financial Accounts where the Client is in Trigger.NewMap keySet, which contains Account Ids
    List<Financial_Account__c> finAccts = [
        SELECT Id, Client__c, Wealth_Advisor_Lookup__c, Portfolio_Manager_Lookup__c, Trust_Officer_Lookup__c,
               Other_Team_Member_1_Lookup__c, Other_Team_Member_2_Lookup__c
        FROM Financial_Account__c
        WHERE Client__c IN : TriggerNewMap.keySet()];

    //Create a list to hold Financial Accounts to Update
    List<Financial_Account__c> finAcctsToUpdate = new List<Financial_Account__c>();

        //Loop through all Financial Accounts whose Client is in Trigger.New
        for(Financial_Account__c finAcct : finAccts){
            //Check to see if the active Financial Account's is the active Account
            Account acct = TriggerNewMap.get(finAcct.Client__c); //retrieve related Acc from Map

                //If yes, set the Financial Account's Relationship team to = the Account's Relationship Team
                finAcct.Wealth_Advisor_Lookup__c = acct.Wealth_Advisor__c;
                finAcct.Portfolio_Manager_Lookup__c = acct.Portfolio_Manager_Lookup__c;
                finAcct.Trust_Officer_Lookup__c = acct.Trust_Officer__c;
                finAcct.Other_Team_Member_1_Lookup__c = acct.Other_Team_Member_1__c;
                finAcct.Other_Team_Member_2_Lookup__c = acct.Other_Team_Member_2__c;

                //Add the Financial Account to the list of Financial Accounts to Update
                finAcctsToUpdate.add(finAcct);

        }
    //Update the Financial Accounts
    update finAcctsToUpdate;
}
}
7
  • Thanks. This is actually a different trigger/class than the one you helped me with yesterday. This one is on Updates to Account, whereas yesterday's was on Updates/Inserts to Financial Account. Does the above code still apply? Thanks again! Commented Oct 10, 2012 at 14:46
  • O right, I didnt realise that at first :) I've added an edit - do you want to see if this works better. Basically passing in the trigger.NewMap() from your trigger will make it a lot more efficient. I'm assuming here that you were using the Account Id to match on. Commented Oct 10, 2012 at 15:17
  • Sweet :) You're more than welcome. (The only further optimisation I'd recommend is comparing if the values of the lookups have changd (Portfolio Manager, etc), before assigning them. But if it guaranteed they will, there's no point doing that) Commented Oct 10, 2012 at 15:56
  • It's actually more likely that they won't be changing very often. What's the benefit of comparing them first? Would I just do this with an IF statement? Commented Oct 10, 2012 at 15:58
  • Yes, and only add it to the list of Financial Accounts to be updated, only if one of the lookups is changing, no point updating or else. This is optimised enough though. :) Commented Oct 10, 2012 at 16:02
1

Trigger looks good and it can handle really large chunks of data .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .