+ Start a Discussion
LloydSilverLloydSilver 

Help fixing bulkification issue

I'm running into a bulk update issue on the trigger below. I thought that I properly structured the apex for bulkification but apparently I didn't and I'm stuck. I'd really appreciate some help. Thanks.

 

trigger UpdateAgentCaseCommissionOnCaseUpdate on Insurance_Case__c (after update) {

  Map<Id, Insurance_Case__c> cases = new Map<Id, Insurance_Case__c>();
  Map<Id, Insurance_Case__c> casesstatus = new Map<Id, Insurance_Case__c>();
  Agent_Cases_Commissions__c[] accToUpdate = new Agent_Cases_Commissions__c[0];
  
  for(Insurance_Case__c record: Trigger.new){
    Insurance_Case__c oldcase = Trigger.oldMap.get(record.Id);
    if(record.Status__c != oldcase.Status__c && record.Status__c != 'Submitted'){
      casesstatus.put(record.Id, record);
    } else {
      cases.put(record.Id, record);
    }
  }
  
  Agent_Cases_Commissions__c[] accstatus = [SELECT Id, Insurance_Case__c, Contract_Type__c FROM Agent_Cases_Commissions__c WHERE Insurance_Case__c IN :casesstatus.keyset() AND Contract_Type__c = 'Agent Contract'];
  Agent_Cases_Commissions__c[] accnostatus = [SELECT Id, Insurance_Case__c, Contract_Type__c FROM Agent_Cases_Commissions__c WHERE Insurance_Case__c IN :cases.keyset() AND Contract_Type__c = 'Agent Contract'];
  
  for(Agent_Cases_Commissions__c acc :accstatus){
    acc.Case_Status_Change__c = TRUE;
    accToUpdate.add(acc);
  }
  
  for(Agent_Cases_Commissions__c acc2 :accnostatus){
    accToUpdate.add(acc2);
  }
  
  if(!accToUpdate.isEmpty())
  update accToUpdate;

}

 

Starz26Starz26

Just because it errors with too many SOQL on your code does not mean your code was the issue.

 

You will need to debug the entire transaction and follow it to see what keeps calling your code inefficiently.

LloydSilverLloydSilver

Okay, I didn't actually think about it in those terms.

 

What is actually initiating whatever is causing the error is a test class created by someone else. Here is the code to that in case something jump out as obviously a problem.

 

@isTest
private class TestCommissionExtension
{  
  static testMethod void commissionExtensiontest()
      
    { 
     
        
       Insurance_Case__c Rec=new Insurance_Case__c();
       
     Rec.Modal_Premium__c = 500;
       Rec.Target_Premium__c = 900;
       Rec.X1035_Received_Date__c = System.today()+400;
       Rec.X1035_Exchange_Amount__c = 300;
       Rec.Lump_Sum_Received_Date__c = System.today();
       Rec.Lump_Sum_Amount__c= 300;
       Rec.Premium_Mode__c = 'Monthly';
       Rec.Status__c = 'inforce';
       Rec.Policy_Date__c = System.today();
       
       insert Rec;
       
       
       //New Carrier__c (C)
       // New Carrier_Contract__c ((D, D refers to C)
       
       
    Id Carrier = [Select Carrier__c from Carrier_Contract__c where not Contract_Description__c   like '%Imeriti%' limit 1].Id;
    
      Agent_Cases_Commissions__c ACC = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec.Id,Carrier_Contract__c = Carrier);
     
      ACC.FYC_Rate__c = 95;  
      ACC.Excess_Commission_Rate__c = 1.75;
      ACC.Case_Split__c = 100;
      ACC.Commission_Group__c = 1;
      
      insert ACC;
      
   //   RecordType RT = new RecordType( Name='TestImeriti' , sObjectType='Carrier_Contract__c')


  //insert RT;
      
      //New Carrier__c (C1 Recordtype=RT.Id)
       // New Carrier_Contract__c ((D1, D1 refers to C1, DR  Name = 'Imerti')
       
      
      Id Carrier_a = [Select Carrier__c from Carrier_Contract__c where Contract_Description__c like '%Imeriti%'  limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_a = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec.Id,Carrier_Contract__c = Carrier_a);
     
      ACC_a.FYC_Rate__c = 75;
      ACC_a.Excess_Commission_Rate__c = 2.75;
      ACC_a.Case_Split__c = 100;
      ACC_a.Commission_Group__c = 1;
      
      insert ACC_a;
      
       ApexPages.StandardController stdController = new ApexPages.StandardController (Rec);
      CommissionExtension controller = new CommissionExtension(stdController);
      
      controller.calcComm();
       
        controller.getGreeting();
      
        Rec.X1035_Received__c = true;
        update Rec;
        
        Rec.Lump_Sum_Received__c = true;
        update Rec;
        
       
        Insurance_Case__c Rec1=new Insurance_Case__c();
       
     Rec1.Modal_Premium__c = 300;
       Rec1.Target_Premium__c = 5000;
       Rec1.X1035_Received_Date__c = System.today();
       Rec1.Lump_Sum_Received_Date__c = System.today() + 10;
       Rec1.X1035_Exchange_Amount__c = 1000;
       Rec1.Premium_Mode__c = 'Semi-Annual';
       Rec1.Policy_Date__c = System.today();
       Rec1.Lump_Sum_Amount__c = 1000;
       Rec1.Status__c = 'inforce';
       
      insert Rec1;
      //Use C
    Id Carrier_1a = [Select Carrier__c from Carrier_Contract__c  where not Contract_Description__c  like '%Imeriti%'limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_1a = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec1.Id,Carrier_Contract__c = Carrier_1a);
     
      ACC_1a.FYC_Rate__c = 5;
      ACC_1a.Excess_Commission_Rate__c = 0.75;
      ACC_1a.Case_Split__c = 400;
      ACC_1a.Commission_Group__c = 2;
      
      insert ACC_1a;
      //Use C1
      Id Carrier_1b = [Select Carrier__c from Carrier_Contract__c where Contract_Description__c like '%Imeriti%'  limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_1b = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec1.Id,Carrier_Contract__c = Carrier_1b);
     
      ACC_1b.FYC_Rate__c = 75;
      ACC_1b.Excess_Commission_Rate__c = 2.75;
      ACC_1b.Case_Split__c = 100;
      ACC_1b.Commission_Group__c = 1;
      
      insert ACC_1b;
     
       ApexPages.StandardController stdController1 = new ApexPages.StandardController (Rec1);
      CommissionExtension controller1 = new CommissionExtension(stdController1);
      
        controller1.calcComm();
      
        controller1.getGreeting();
      
        Rec1.X1035_Received__c = true;
        update Rec1;
        
                
        Rec1.Lump_Sum_Received__c = true;
        update Rec1;
        
        Insurance_Case__c Rec2=new Insurance_Case__c();
       
     Rec2.Modal_Premium__c = 1500;
       Rec2.Target_Premium__c = 900;
       Rec2.X1035_Received_Date__c = System.today()+400;
       Rec2.X1035_Exchange_Amount__c = 1000;
       Rec2.Premium_Mode__c = 'Quarterly';
       Rec2.Lump_Sum_Amount__c = 1000;
       Rec2.Status__c = 'inforce';
       Rec2.Policy_Date__c = System.today();
       
       
      insert Rec2;
      //Use C
    Id Carrier_2a = [Select Carrier__c from Carrier_Contract__c  where not Contract_Description__c  like '%Imeriti%'limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_2a = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec2.Id,Carrier_Contract__c = Carrier_2a);
     
      ACC_2a.FYC_Rate__c = 45;
      ACC_2a.Excess_Commission_Rate__c = 1.75;
      ACC_2a.Case_Split__c = 100;
      ACC_2a.Commission_Group__c = 5;
      
      insert ACC_2a;
      //Use C1
      Id Carrier_2b = [Select Carrier__c from Carrier_Contract__c where Contract_Description__c like '%Imeriti%'  limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_2b = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec1.Id,Carrier_Contract__c = Carrier_2b);
     
      ACC_2b.FYC_Rate__c = 65;
      ACC_2b.Excess_Commission_Rate__c = 2.75;
      ACC_2b.Case_Split__c = 100;
      ACC_2b.Commission_Group__c = 1;
      
      insert ACC_2b;
      
       ApexPages.StandardController stdController2 = new ApexPages.StandardController (Rec2);
      CommissionExtension controller2 = new CommissionExtension(stdController2);
      
        controller2.calcComm();
      
        controller2.getGreeting();
      
        Rec2.X1035_Received__c = true;
        update Rec2;
        
        Rec2.Lump_Sum_Received__c = true;
        update Rec2;
        
        Insurance_Case__c Rec3=new Insurance_Case__c();
       
     Rec3.Modal_Premium__c = 3500;
       Rec3.Target_Premium__c = 5900;
       Rec3.X1035_Received_Date__c = System.today();
       Rec3.X1035_Exchange_Amount__c = 1000;
       Rec3.Premium_Mode__c = 'Annual';
       Rec3.Status__c = 'inforce';
       Rec3.Policy_Date__c = System.today();
       Rec3.Lump_Sum_Amount__c = 1000;
       
      insert Rec3;
      
      Id Carrier_3a = [Select Carrier__c from Carrier_Contract__c where not Contract_Description__c  like '%Imeriti%' limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_3a = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec3.Id,Carrier_Contract__c = Carrier_3a);
     
      ACC_3a.FYC_Rate__c = 85;
      ACC_3a.Excess_Commission_Rate__c = 1.75;
      ACC_3a.Case_Split__c = 100;
      ACC_3a.Commission_Group__c = 1;
      
      insert ACC_3a;
      
      Id Carrier_3b = [Select Carrier__c from Carrier_Contract__c where Contract_Description__c like '%Imeriti%'  limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_3b = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec3.Id,Carrier_Contract__c = Carrier_3b);
     
      ACC_3b.FYC_Rate__c = 55;
      ACC_3b.Excess_Commission_Rate__c = 2.75;
      ACC_3b.Case_Split__c = 100;
      ACC_3b.Commission_Group__c = 1;
      
      insert ACC_3b;
      
       ApexPages.StandardController stdController3 = new ApexPages.StandardController (Rec3);
      CommissionExtension controller3 = new CommissionExtension(stdController3);
      
        controller3.calcComm();
      
        controller3.getGreeting();
      
      
        Rec3.X1035_Received__c = true;
        update Rec3;
        
        Rec3.Lump_Sum_Received__c = true;
        update Rec3;
        
        Insurance_Case__c Rec4=new Insurance_Case__c();
       
     Rec4.Modal_Premium__c = 1000;
       Rec4.Target_Premium__c = 200;
       Rec4.X1035_Received_Date__c = System.today();
       Rec4.X1035_Exchange_Amount__c = 1000;
       Rec4.Premium_Mode__c = 'Single Premium';
       Rec4.Policy_Date__c = System.today();
       Rec4.Status__c = 'inforce';
       Rec4.Lump_Sum_Amount__c = 1000;
       
      insert Rec4;
      
      Id Carrier_4a = [Select Carrier__c from Carrier_Contract__c where not Contract_Description__c  like '%Imeriti%' limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_4a = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec4.Id,Carrier_Contract__c = Carrier_4a);
     
      ACC_4a.FYC_Rate__c = 95;
      ACC_4a.Excess_Commission_Rate__c = 1.75;
      ACC_4a.Case_Split__c = 100;
      ACC_4a.Commission_Group__c = 1;
      
      insert ACC_4a;
      
      Id Carrier_4b = [Select Carrier__c from Carrier_Contract__c where Contract_Description__c like '%Imeriti%'  limit 1].Id;
    
      Agent_Cases_Commissions__c ACC_4b = new Agent_Cases_Commissions__c(Insurance_Case__c = Rec4.Id,Carrier_Contract__c = Carrier_4b);
     
      ACC_4b.FYC_Rate__c = 75;
      ACC_4b.Excess_Commission_Rate__c = 2.75;
      ACC_4b.Case_Split__c = 100;
      ACC_4b.Commission_Group__c = 1;
     
      insert ACC_4b;
      
       ApexPages.StandardController stdController4 = new ApexPages.StandardController (Rec4);
      CommissionExtension controller4 = new CommissionExtension(stdController4);
      
        controller4.calcComm();
      
        controller4.getGreeting();
      
      
        Rec4.X1035_Received__c = true;
        update Rec4;
        
        Rec4.Lump_Sum_Received__c = true;
        update Rec4;
        
        // TO DO: implement unit test
    }
   
    
}