You need to sign in to do that
Don't have an account?

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
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
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
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:
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
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
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