function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aaron2010Aaron2010 

Bulkify

Hello all,

                I am a newbie to apex development. Can any one help me in bulkifying this trigger.

 

  trigger PopulateRegistrantGroup on Event_Registrant_zem__c (before insert, before update) {      

        String s;

        Id id1;

         for (Event_Registrant_zem__c e :Trigger.new)

          {
                 id1 = e.registrant_type__c;
                s = [select id,name from Registrant_Type_zem__c where id = :id1].name;
        
         if(e.Registrant_type_group__c == NULL)
          {
            e.Registrant_type_group__c = [select epz.Id from Event_permission_zem__c epz where epz.zimmer_event__c = :e.zimmer_event__c and epz.type__c = 'Event Capacity' and epz.Registrant_Types__c INCLUDES (:s)][0].id;
          }
       }

}

 

Any help is appriciated. Thanks in advance.

tmatthiesentmatthiesen

You need to move the SOQL statements outside of the FOR loop and leverage the Trigger.new as a SET in your query.  Take a look a the following documentation:

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_patterns_bulk.htm

jeffdonthemic2jeffdonthemic2
krishnagkrishnag

hi here is the below syntax for the asynchronous class which handles bulk processing.

 

 

Trigger
trigger UpdateBrokerOnAccount on Account (before update)
{
    list<String> AccountIds = new list<String>();
    if (!ProcessorControl.inFutureContext) {

    for(Account a : Trigger.new) {

             AccountIds.add(a.Id);  
}
   
 updateBrokerFirm.updateBrokerFirmMethod(AccountIds);
}
}

Class for Asynchronous call on trigger
public class updateBrokerFirm {

@future
public static void updateBrokerFirmMethod(list<String> AccountIds){

    Map<Id,Policy__c> policyMap = new Map<Id,Policy__c> ();
   Map<Id, Submission__c> submissionMap = new Map<Id, Submission__c>();
   
   for( Policy__c policy : [SELECT Id,Distributor_Name__c, Company_Name__c FROM Policy__c 
                                                 WHERE Distributor_Name__c != null and 
                                                 Company_Name__c IN :AccountIds Order by Policy_Premium__c DESC])
{
    if(!policyMap.containsKey(policy.Company_Name__c))
    {
        policyMap.put(policy.Company_Name__c,policy);
    }   

}

for( Submission__c submission : [SELECT Id,Producer_Company__c, Account_Name__c FROM Submission__c 
                                                 WHERE Producer_Company__c != null and 
                                                 Account_Name__c IN :AccountIds Order by System_Bound_Premium__c DESC])
{
    if(!submissionMap.containsKey(submission.Account_Name__c))
    {
        submissionMap.put(submission.Account_Name__c,submission);
    }   


}
list<Account> AllAccounts = [select id,Broker_Firm__c from Account where Id IN :AccountIds];

for(Account temp :AllAccounts){
 
  if(temp.Broker_Firm__c == null){
            if(policyMap.containsKey(temp.Id)) 
    {
               temp.Broker_Firm__c = policyMap.get(temp.Id).Distributor_Name__c;
               
            }
    else
    {
         if(submissionMap.containsKey(temp.Id)) 
        {
                   temp.Broker_Firm__c = submissionMap.get(temp.Id).Producer_Company__c;
               
                    }
    }
    
        }
  
  }
ProcessorControl.inFutureContext = true;
update AllAccounts;
}
}

 this is the example for bulkifying the triggers.

 

Aaron2010Aaron2010

Thank you all for the responses. I will try and let you all know what happened.