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
Vishnu_SFDCVishnu_SFDC 

Too many soql error :101 on different trigger when executing a apex class

I am trying to execute the following class from a button. when i set the limit to 9 it wors fine. When i remove limit on the 1st query it throws error on other triggers.

global class DERedistribute{
@future
webservice static void DERedistribute(){
List<Lead>  l = [SELECT Name,Times_The_Lead_Is_Distributed__c,Hawaii__c,SV_Priority_Hidden__c, Id, AssignToUserId_Hidden__c FROM Lead WHERE IsConverted =false and Hawaii__c = false and To_Be_Distributed__c=true and DE_Synched__c=true and Is_Distributed__c =True];
integer x=0;
List<Team1__c> L1 = [SELECT Name,CheckIn__c From Team1__c Where CheckIn__c = True]; 
  integer i = L1.size();
integer z=1;
  for(Team1__c L2 :[SELECT Name,CheckIn__c,UserId__c From Team1__c] )
  {
  If(L2.CheckIn__c == True)
    {
     L2.UserId__c = z++;
 
     }
  else
  {
  L2.UserId__c =0;

  }
    update L2; 
  }
  for(Lead e : l)
  {
   x++;
  decimal y =  e.Times_The_Lead_Is_Distributed__c;
  e.AssignToUserId_Hidden__c = math.MOD(x, i) + 1;
  e.Is_Distributed__c =True;
   e.Times_The_Lead_Is_Distributed__c = y +1;
   update e;
  }

}
}


Phillip SouthernPhillip Southern
Are you getting too many soql error or dml statements?  I would suspect DML since you have update statements inside a for loop.  Consider using a List and adding updated values to the List...then performing update on the List after the loop is complete.
Vishnu_SFDCVishnu_SFDC
I updated the For loop as follows. But It still throws the same error.


for(Lead e : l)
  {
   x++;
  transient decimal y =  e.Times_The_Lead_Is_Distributed__c;
  e.AssignToUserId_Hidden__c = math.MOD(x, i) + 1;
  AssignToUserId.add(e.AssignToUserId_Hidden__c);  
  e.Is_Distributed__c =True;
  IsDistributed.add(e.Is_Distributed__c);   
   e.Times_The_Lead_Is_Distributed__c = y +1;
  TimesLeadIsDistributed.add(e.Times_The_Lead_Is_Distributed__c);  
  id.add(e.Id);

      transient decimal d =0;
       Lead lead = new Lead();
        if(AssignToUserId.size()>d)
        lead.AssignToUserId_Hidden__c = AssignToUserId[x-1];
        if(IsDistributed.size()>d)
        lead.Is_Distributed__c = IsDistributed[x-1];
        if(TimesLeadIsDistributed.size()>d)
        lead.Times_The_Lead_Is_Distributed__c = TimesLeadIsDistributed[x-1];
        L3.add(lead);
        lead.Id = id[x-1];
        d++;
    }
    if(L3.size()>0)
    {
        Schema.SObjectField f = Lead.Fields.Id;
           Database.UpsertResult[] UpdateResult = Database.UPSERT(L3,f,false);
       
            }