+ Start a Discussion
SandrineSandrine 

Populating the triger data at the beginning of the handler class

Hello,

I have trigger in below pattern

One Account trigger
- Calls Trigger handler

Account Triger handler
- makes distinhuish betwen before ,after, insert, updae
- have each fucntion , which call class to execute actions/functions

I want to improve the trigger to get the data at the beinning of the handler class and execute the trigge logic by using the map 
in other owrds, SOQL to get the user and Account data in beginning and store in map. and use the map in the rest of the account handler.

where i could start from, please 

PriyaPriya (Salesforce Developers) 

Hi Sandrine,

I have not understand the requirement completely. Can you provide your code and then elaborate your requirement for better understanding ?

Regards,

Priya Ranjan
Salesforce Support

SandrineSandrine
Trigger:

    trigger AccountTrigger on Account(before insert, before update)  
    {
        AccountTriggerHandler handler = new  AccountTriggerHandler();
        
        handler.updateUser(Trigger.new);  // Call method for update account field.

    }

Trigger Handler Class:


    public class AccountTriggerHandler
    {

//TO DO: contruct the account list and pass this map to othr functions

           public void updateUser(List<Account> accList)
           {
                  Set<Id> masterIds = new Set<Id>();
                  
                  for(Account accObj : accList)
                  {
                    if(accObj.Master__c != null)
                       masterIds.add(accObj.Master__c);
                  }
            
                    Map<Id,Master__c> masterMap = new Map<Id,Master__c>([Select Id, test_user1__c from Master__c where Id IN : masterIds]);
                     
                     
                  for(Account acct : accList)
                  {
            
                    if(masterMap.containsKey(acct.Master__c))
                    {
                       Master__c masterObj = masterMap.get(acct.Master__c);
                  
                        if(acct.test_user1__c == null)
                
                           acct.test_user1__c = masterObj.test_user1__c;
                    }
                  }
           }
    }

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sandrine,

Please refer the below link will help you to proceed further.

https://niksdeveloper.com/salesforce/apex-trigger-best-practices-all-in-one/

Thanks!!
SandrineSandrine
i am looking for populating queries in beginning but not best practices