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
MaxaMaxa 

System.LimitException: Too many DML statements: 151

i Have an issue with my trigger, it is very simple one, basically it takes credit card type and expiry date from payment and put it up one level on Invoice, for reporting purposes. it works fine when a single payment is inserted however when i do bulk upload of new payments i get this error

System.LimitException: Too many DML statements: 151

 

here is my code below any help wpuld be greatly apreciated

trigger CC_Exp on Payment__c (after insert, after update) {
Set<id> objSet = new Set<id>();  

for(Payment__c objOpp:trigger.new)

{

objSet.add(objOpp.Invoice__c);

}

List<Invoice__c> myInvoice = [select Id,Status__c from Invoice__c where Id in: objSet];

for(Payment__c objOpp:Trigger.new)

{

for(Invoice__c objAcc: myInvoice)

{

if (objOpp.Payment_Method__c == 'Credit Card') {

{

objAcc.CC_Expiry_Date__c = objOpp.Expiry_Date__c;//
objAcc.Creditcard_Type__c = objOpp.Credit_card_Type__c;//
update objAcc;
}}}
}}

 

shra1_devshra1_dev

This is because you did not handle for the bulk insertion of records.

 

just take list of invoices add your invoices (which has to be updated). Do the updation out side the for loop.

 

 

list<invoice__c> lstInvs = new list<invoice__c>(); declare it below the objSet declaration

 

then in the for loop

 

objAcc.CC_Expiry_Date__c = objOpp.Expiry_Date__c;

objAcc.Creditcard_Type__c = objOpp.Credit_card_Type__c;

 

lstInvs.add(objAcc);

 

then at last update the list (outside for loops)

 

update lstInvs;

 

 

 

regards,

Shravan

MaxaMaxa

i think i'm doing somethign wrong here

now i get error  Duplicate ID in List, here is my trigger

trigger CC_Exp on Payment__c (after insert, after update) {
Set<id> objSet = new Set<id>();  
list<invoice__c> lstInvs = new list<invoice__c>(); 
for(Payment__c objOpp:trigger.new)

{

objSet.add(objOpp.Invoice__c);

}

List<Invoice__c> myInvoice = [select Id,Status__c from Invoice__c where Id in: objSet];

for(Payment__c objOpp:Trigger.new)

{

for(Invoice__c objAcc: myInvoice)

{

if (objOpp.Payment_Method__c == 'Credit Card') {

{

objAcc.CC_Expiry_Date__c = objOpp.Expiry_Date__c;//
objAcc.Creditcard_Type__c = objOpp.Credit_card_Type__c;//

lstInvs.add(objAcc);


}}}
}
update lstInvs;


}

 

 

shra1_devshra1_dev

It is better to check the Payment_Method__c == 'credit card' in the trigger it self.

 

if(objOpp.Payment_Method__c == 'Credit Card')

{

objSet.add(objOpp.Invoice__c);

}

 

and remove the first for loop i.e, for(Payment__c objOpp:Trigger.new)

 

 

Regards,

Shravan

MaxaMaxa

if i remove the first loop it doe not let me compile, says that objOpp.Invoice__c does not exists.

 i tried differnt way declaring it such as payment__c objSet;

but it tells me duplicate variables.

Rahul SharmaRahul Sharma

Can you correct me here.

 

Payment is Child having field on it Invoice__c which is Lookup/Masterdetail to its Parent Invoice.

MaxaMaxa

thats corect payment is a child of Invoice, and i'm trying to bring up some info from apyment details to invoice level

Rahul SharmaRahul Sharma

try this:

 

trigger CC_Exp on Payment__c (after insert, after update) 
{
   List<Invoice__c> lstInvoice = new List<Invoice__c>();
   for(Payment__c objPayment : [Select Name, Id, Invoice__r.CC_Expiry_Date__c, Invoice__r.Creditcard_Type__c , Expiry_Date__c, Credit_card_Type__c from Payment__c where Id IN: Trigger.new and Payment_Method__c = 'Credit Card'])
   {
      lstInvoice.add(new Invoice__c(CC_Expiry_Date__c = objPayment.Invoice__r.CC_Expiry_Date__c, Creditcard_Type__c = objPayment.Invoice__r.Creditcard_Type__c));
   }
   if(!lstInvoice.isEmpty())
   {
      update lstInvoice;
   }
}

 Hope it helps.

MaxaMaxa

now i get error

MISSING_ARGUMENT, Id not specified in an update call.

 

Rahul SharmaRahul Sharma

Give this a try:

 

trigger CC_Exp on Payment__c (after insert, after update) 
{
   Map<Id, Invoice__c> mapInvoice = new Map<Id, Invoice__c>([Select Id, CC_Expiry_Date__c, Creditcard_Type__c, Name from Invoice__c
                                         where Id IN (Select Id from Invoice__c where Id in: Trigger.new())]);

   List<Invoice__c> lstInvoice = new List<Invoice__c>();
   for(Payment__c objPayment : [Select Name, Id, Invoice__r.CC_Expiry_Date__c, Invoice__r.Creditcard_Type__c , 
   Expiry_Date__c, Credit_card_Type__c from Payment__c where Id IN: Trigger.new and Payment_Method__c = 'Credit Card'])
   {
      if(mapInvoice.Contains(objPayment.Invoice__c))
      {
	 lstInvoice.add(new Invoice__c(Id = objPayment.Invoice__c, CC_Expiry_Date__c = objPayment.Invoice__r.CC_Expiry_Date__c, Creditcard_Type__c = objPayment.Invoice__r.Creditcard_Type__c));
      }
	
   }
   if(!lstInvoice.isEmpty())
   {
      update lstInvoice;
   }
}

 

shra1_devshra1_dev

Hi Maxa,

 

Sorry for the late response.

 

So you want to take some information from Payment and update it on Invoice.

 

But If there are more than one payments inserted or updated then which payment information has to updated on Invoice did you think of that?

 

 

Regards,

Shravan

Rahul SharmaRahul Sharma

I think with this logic, Invoice will be updated with only the recent payment.

is this your requirement.

MaxaMaxa

Hi yes you are correct i only want mroe recent credit card info, old ones are irrelevant, with this new code i get compile error

The inner and outer selects should not be on the same object type at line 3 column 61

 

 

Rahul SharmaRahul Sharma

ohh, got it. Just change the inner query to Payment__c instead of Invoice:

 

Map<Id, Invoice__c> mapInvoice = new Map<Id, Invoice__c>([Select Id, CC_Expiry_Date__c, Creditcard_Type__c, Name from Invoice__c where Id IN (Select Id from Payment__c where Id in: Trigger.new())]);

 

MaxaMaxa

thans for htat, ihave alredy played around swaping them places but get this erro

 Error: Compile Error: The selected field 'Id' in the subquery and the left operand field in the where expression in the outer query 'Id' should point to the same object type at line 4 column 61

 

looks like catch 22 :)

Johnie JonesJohnie Jones

Hi,

 

have you succeeded in solving your problem? I'm unfortunatelly facing the same issue and cannot move any forward :(

DML2020DML2020

Hi. 

What was the final fix in this case? I'm currently receiving a similar error where I see for a user/ org ID...


Failed to process batch for class 'BatchPaymentApplicator' for job id '7074z00007Ky901'
caused by: System.LimitException: NU:Too many DML statements: 151

Class.NU.TransactionGroupAutoNumberManager.generateNumbers: line 39, column 1
Class.NU.TransactionGroupAutoNumberManager.generateNumber: line 52, column 1
Class.NU.DueToDueFromTransactionGenerator.generate: line 53, column 1
Class.NU.TransactionGenerator2.generateTransactions: line 196, column 1
Class.NU.TransactionGenerator2.generate: line 87, column 1
Class.NU.TransactionGenerator2.generate: line 124, column 1
Class.NU.TransactionGenerator.generate: line 14, column 1
Class.BatchPaymentApplicator.createTransactions: line 507, column 1
Class.BatchPaymentApplicator.execute: line 200, column 1

Would be happy to learn where to navigate to solve this issue.