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
padmini sspadmini ss 

Error:Too Many DML Statements: 151, but it was bulkify only

This is my trigger, 
trigger CreateOppLineItemNumber on OpportunityLineItem (after insert, before delete,after update) 
{
if(trigger.isInsert){
if(HandelRecurssiveTriggerOppLineItem.flag!=true){

createOppLineItemNumber.InsertingOppLineItems(trigger.new);
HandelRecurssiveTriggerOppLineItem.flag=true;
}
}
if(trigger.isUpdate ){
if(HandelRecurssiveTriggerOppLineItem.flag!=true){

createOppLineItemNumber.UpdatingOppLineItems(trigger.old);
HandelRecurssiveTriggerOppLineItem.flag=true;
}
}
if(trigger.isDelete){
createOppLineItemNumber.DeletingOppLineItems(trigger.old);
}
}

Apex: calss
public class createOppLineItemNumber{

map<Id,Opportunity> mapopps = new map<Id,Opportunity>();

 public static void InsertingOppLineItems( OpportunityLineItem  [] OLits){
 List <OpportunityLineItem> UpdateOli = New List <OpportunityLineItem>();
 set<id> setopids = new set<id>();
for(OpportunityLineItem oli : OLits)
  {

   if(oli.opportunityId!=null)
   Setopids.add(oli.opportunityId);
 }
  map<Id, Opportunity> mapOpp= new map<Id, Opportunity>([SELECT Id,DeletedOpplineHold__c,totlanonnullcounts__c from Opportunity Where Id IN :setopids ]);

   integer i=0;

  for(OpportunityLineItem oplin :OLits)
  {

   integer res=integer.valueof((mapopp.get(oplin.opportunityId).totlanonnullcounts__c!=null?mapopp.get(oplin.opportunityId).totlanonnullcounts__c:0)+1);
   res=res+integer.valueof((mapopp.get(oplin.opportunityId).DeletedOpplineHold__c!=null?mapopp.get(oplin.opportunityId).DeletedOpplineHold__c:0));
  System.debug('DEleted count :'+(mapopp.get(oplin.opportunityId).DeletedOpplineHold__c));
  if(oplin.Item_Number__c==null)
  {

   res+=i;
   OpportunityLineItem  op=new OpportunityLineItem (id=oplin.id,Item_Number__c=res*10);
   UpdateOli.add(op);
   i++;
  }

}

if(UpdateOli.size()>0)
update UpdateOli;
        
 }//InsertingOppLineItems CLOSED


public static void UpdatingOppLineItems( OpportunityLineItem  [] OLit){
set<id> setopids = new set<id>();
List <OpportunityLineItem> UpdateOli = New List <OpportunityLineItem>();
for(OpportunityLineItem oli : OLit)
  {
    if(oli.opportunityId!=null)
    Setopids.add(oli.opportunityId);
 }

map<Id, Opportunity> mapOpp= new map<Id, Opportunity>([SELECT Id,DeletedOpplineHold__c,totlanonnullcounts__c from Opportunity Where Id IN :Setopids]);
integer i=0;

for(OpportunityLineItem oplin :OLit)
{
  integer res=integer.valueof((mapopp.get(oplin.opportunityId).totlanonnullcounts__c !=null?mapopp.get(oplin.opportunityId).totlanonnullcounts__c :0)+1);
  res=res+integer.valueof((mapopp.get(oplin.opportunityId).DeletedOpplineHold__c!=null?mapopp.get(oplin.opportunityId).DeletedOpplineHold__c:0));
  System.debug('**********==== :' +mapopp.get(oplin.opportunityId).DeletedOpplineHold__c);

//System.debug('R E S  :' +res);
  if(oplin.Item_Number__c==null)
  {
    res+=i;
    System.debug('R E S  :' +res);
    OpportunityLineItem  op=new OpportunityLineItem (id=oplin.id,Item_Number__c=res*10);
    UpdateOli.add(op);
    i--;
  }
 }

if(UpdateOli.size()>0)
update UpdateOli;

}

     

}

i am updating the records via Workbench, its throwing an error...

but i added to a list , and after that i am updating the list, not in the loop...pls give me the reply urgent pls..
 

BalajiRanganathanBalajiRanganathan
Ideally you should have done your logic in before insert and before update.  if you do that then you don't need to call the update.

i think you code is executing recursively that causes the error.

you could also try to put the flag which avoids recursive call before calling the logic for all operation. i have given a sample below for update.
if(trigger.isUpdate ){
if(HandelRecurssiveTriggerOppLineItem.flag!=true){
HandelRecurssiveTriggerOppLineItem.flag=true;
createOppLineItemNumber.UpdatingOppLineItems(trigger.old);
}

But as i said before, you could achive your requiment using before triggers instead of after triggers