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;
}
}
Attribution to: jackerman09
Possible Suggestion/Solution #1
Trigger looks good and it can handle really large chunks of data .
Attribution to: Mohith Shrivastava
Possible Suggestion/Solution #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;
}
}
Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2140