I am getting this error
Error: Maximum trigger Depth.
I am getting the above trigger on After Update
trigger PaymentCalculations on Payment__c (after insert,after update) {
system.debug('Payment Calculation Trigger');
Set<Id> LoanIds = new Set<Id>();
for(Payment__c pm : trigger.new){
if(pm.Loan__c!=null){
LoanIds.add(pm.Loan__c);
}
}
if(!LoanIds.isEmpty()){
List<Payable__c> payables = new List<Payable__c>();
Map<Id,Loan__c> deals = new Map<Id,Loan__c>(
[select Id,Name,(select id,Syndicator_Fee__c, of_Options__c,Syndicating_Account__c from Syndicators__r where status__c = 'Approved'),Account__c from Loan__c where Id In :LoanIds]
);
for(Payment__c pm : trigger.new){
if(pm.Loan__c !=null){
Loan__c deal = deals.get(pm.Loan__c);
Decimal amountToSend;
for(Syndicator__c synd :deal.Syndicators__r){
if(pm.Amount__c!=null && synd.of_Options__c!=null && synd.Syndicator_Fee__c!=null){
amountToSend = pm.Amount__c * synd.of_Options__c * (100-synd.Syndicator_Fee__c)/10000;
}
system.debug('Syndication Amount------>'+amountToSend+'syndicator Option'+synd.of_Options__c);
String PayableName=deal.Name+' '+'-'+' '+'$'+pm.Amount__c;
if(PayableName.length()>80)
{
PayableName.substring(0,80);
}
Payable__c payable = new Payable__c (Payment__c=pm.id,Syndicator__c = synd.Id,Deal__c = deal.Id,Name=PayableName, payment_to_send__c = amountToSend, Syndicator_Account__c=synd.Syndicating_Account__c);
payables.add(payable);
System.debug('------List of Payables----'+payables);
}
}
}
system.debug('Payable Size------->'+payables.size());
if(!payables.isEmpty()){
upsert payables;
}
}
}
}