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
krishnagkrishnag 

how to bulkify a trigger

hi ,

I have a trigger which was working fine till now and when we load data using data loader the trigger is not firing and throwing an exception too many SOQL queries 21.

 

DO i need to write a trigger ti handle large data speerately or is there anyway please suggest me i am pasting my trigger code.

 

 

trigger UpdateRelationShipOnAccount on Account (before update) {

//start of condition 1
if(Trigger.new[0].Company_Relationship__c!='Broker' && Trigger.new[0].Company_Relationship__c!='Employee'){
    System.debug('In if loop');


    list<Policy__c> NonActivePolicy1 = [select Id,Policy_Status__c from Policy__c where 
                                                     Company_Name__c =:Trigger.new[0].Id limit 1];
  // start of condition 2
  if(!NonActivePolicy1.isEmpty()){
      Trigger.new[0].Company_Relationship__c = 'Customer';
  list<Policy__c> ActivePolicy = [select Id,Policy_Status__c from Policy__c where Policy_Status__c ='Active'
                                                     and Company_Name__c =:Trigger.new[0].Id limit 1];
     //Start of condition 3
    if(!ActivePolicy.isEmpty()){
          Trigger.new[0].Relationship_Type__c = 'Current'; 
  
  }
  //END of if 3  
  
  //Start of condition4 1
  else{
        Date temp = System.today()+180;
      list<Policy__c> NonActivePolicy = [select Id,Policy_Status__c,Policy_Expiration_Date__c from Policy__c where 
                                                Company_Name__c =:Trigger.new[0].Id 
                                                          and Policy_Expiration_Date__c >:temp limit 1];
     if(!NonActivePolicy.isEmpty()){
        Trigger.new[0].Relationship_Type__c = 'Current';
        }
        else{
          list<Submission__c> subBondType =  [SELECT Id,Submission_Status__c,Policy_Status__c FROM Submission__c where 
                                                Account_Name__c =:Trigger.new[0].Id and Submission_Status__c ='Bound' limit 1]; 
            //start of condition7 5
            if(!subBondType.isEmpty()){
                 Trigger.new[0].Company_Relationship__c = 'Customer';
                 if(subBondType[0].Policy_Status__c == 'Active')
                 {
                     Trigger.new[0].Relationship_Type__c = 'Current';
                     }
                 else
                 {
                     Trigger.new[0].Relationship_Type__c = 'Former';   
               }}
               else{
               Trigger.new[0].Relationship_Type__c ='Former';
               }
            //end of if 5
         // Start of condition8 3
        
          
        
        
  }    }
//End of else 1
}
//end of if 2

//Start of condition5 2
else{
      list<Submission__c> subType =  [SELECT Id,Submission_Status__c,Policy_Status__c FROM Submission__c where 
                                               Account_Name__c =:Trigger.new[0].Id  limit 1]; 
 //start of condition6 4
 if(!subType.isEmpty()){
      list<Submission__c> subBondType =  [SELECT Id,Submission_Status__c,Policy_Status__c FROM Submission__c where 
                                                Account_Name__c =:Trigger.new[0].Id and Submission_Status__c ='Bound' limit 1]; 
            //start of condition7 5
            if(!subBondType.isEmpty()){
                 Trigger.new[0].Company_Relationship__c = 'Customer';
                 if(subBondType[0].Policy_Status__c == 'Active')
                     Trigger.new[0].Relationship_Type__c = 'Current';
                 else
                     Trigger.new[0].Relationship_Type__c = 'Former';   
               }
            //end of if 5
         // Start of condition8 3
         else {
            Trigger.new[0].Company_Relationship__c = 'Prospect';
            Trigger.new[0].Relationship_Type__c = 'Activity';
         } 
         //END of else 3    
 }
 //end of if 4
// START of condition9 4
else {
    System.debug('In last if loop');
      if(Trigger.new[0].Direct_Markets_Customer__c==True || Trigger.new[0].Z_Programs_Customer__c ==True)
      {
      Trigger.new[0].Company_Relationship__c = 'Programs';
      }
      else{
            Trigger.new[0].Company_Relationship__c = 'Prospect';
            Trigger.new[0].Relationship_Type__c = 'No Activity';    
          }
  }
// End of else 4
}
//END of else 2
  }
  //END of if 1
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
krishnagkrishnag

i tried using the Asynchronous class and  it was working fine.I heard about there is some problem with future calls the dialy limit what is that can anybody explain me that.

All Answers

krishnagkrishnag

i tried using the Asynchronous class and  it was working fine.I heard about there is some problem with future calls the dialy limit what is that can anybody explain me that.

This was selected as the best answer
krishnagkrishnag

got it its 200 method calls per 24 hrs.