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
Silpi roy 16Silpi roy 16 

101 error for the trigger:New

Hi All,

I am getting  error 101 for the below code(Too many SOQL query)

trigger DeleteBillingItem on BillingItem__c (after insert) {
    set<Id> bID = new set<Id>();
    for(BillingItem__c bi:trigger.new){
       bID.add(bi.Id);
    }

    list<BillingItem__c>BItem = new list<BillingItem__c>();
    If(bID.isEmpty() == false){   
       BItem = [select id,BillingDocumentId__c,UnitPrice__c,Sub_Segment__c,Gross__c,NettoAmount2__c from BillingItem__c
                          where Id IN: bID AND BillingDocumentId__c!=Null
                       and (UnitPrice__c=0 OR UnitPrice__c=null) AND  Sub_Segment__c='Undefined'];
    }

    for(BillingItem__c BI : BItem){
     System.debug('BI: '+BI);
       delete BI;
    }

}

Please help!!

Thanks,
Silpi
Best Answer chosen by Silpi roy 16
Silpi roy 16Silpi roy 16
Hi Anas,

Thanks a lot for so quick help...

Getting below error:Error: Compile Error: Variable does not exist: BI at line 19 column 13

trigger DeleteBillingItem on BillingItem__c (after insert) {
    set<Id> bID = new set<Id>();
    for(BillingItem__c bi:trigger.new){
       bID.add(bi.Id);
    }

    list<BillingItem__c>BItem = new list<BillingItem__c>();
    If(bID.isEmpty() == false){   
       BItem = [select id,BillingDocumentId__c,UnitPrice__c,Sub_Segment__c,Gross__c,NettoAmount2__c from BillingItem__c
                          where Id IN: bID AND BillingDocumentId__c!=Null
                       and (UnitPrice__c=0 OR UnitPrice__c=null) AND  Sub_Segment__c='Undefined'];
    }

for(BillingItem__c BI : BItem){
     System.debug('BI: '+BI);
     BItem.add(BI); 
}
if(BItem.size()>0){
     DELETE BI;
}
}

Please advise

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Silpi,

Greetings to you!

You are getting 101 error because you are using DML inside for loop. According to Salesforce doc,
  • Minimize the number of data manipulation language (DML) operations by adding records to collections and performing DML operations against these collections.
  • Minimize the number of SOQL statements by preprocessing records and generating sets, which can be placed in single SOQL statement used with the IN clause.
So, instead of:

for(BillingItem__c BI : BItem){
     System.debug('BI: '+BI);
       delete BI;
    }

Use this:
 
List<BillingItem__c> bitem = new List<BillingItem__C>();
for(BillingItem__c BI : BItem){
     System.debug('BI: '+BI);
     bitem.add(BI); 
}
if(bitem.size()>0){
     DELETE BI;
}

Please refer to below links for more information:
https://help.salesforce.com/articleView?id=000181404&type=1

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Silpi roy 16Silpi roy 16
Hi Anas,

Thanks a lot for so quick help...

Getting below error:Error: Compile Error: Variable does not exist: BI at line 19 column 13

trigger DeleteBillingItem on BillingItem__c (after insert) {
    set<Id> bID = new set<Id>();
    for(BillingItem__c bi:trigger.new){
       bID.add(bi.Id);
    }

    list<BillingItem__c>BItem = new list<BillingItem__c>();
    If(bID.isEmpty() == false){   
       BItem = [select id,BillingDocumentId__c,UnitPrice__c,Sub_Segment__c,Gross__c,NettoAmount2__c from BillingItem__c
                          where Id IN: bID AND BillingDocumentId__c!=Null
                       and (UnitPrice__c=0 OR UnitPrice__c=null) AND  Sub_Segment__c='Undefined'];
    }

for(BillingItem__c BI : BItem){
     System.debug('BI: '+BI);
     BItem.add(BI); 
}
if(BItem.size()>0){
     DELETE BI;
}
}

Please advise
This was selected as the best answer
Khan AnasKhan Anas (Salesforce Developers) 
Oh sorry, my bad!

Instead of DELETE BI;

Use this: DELETE Bitem;

I hope it helps you.

Kindly mark this as solved if the information was helpful.

Regards,
Khan Anas
Silpi roy 16Silpi roy 16
It Helped Thanks a lot