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){

        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;
                    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);
                    System.debug('------List of Payables----'+payables);
        system.debug('Payable Size------->'+payables.size());

            upsert payables;

  • use Recursive trigger – SFDC Sep 2 '16 at 12:25

This is likely because you are updating Payment__c objects in your trigger which is causing the same trigger to run again which is then updating again and so on. The best way to avoid this problem is to think through the logic so that the records that you changed the first time are filtered out the second time the trigger runs (based on values in the object) because the work is already done.

(Relying on the static boolean flag set/check approach can yield invalid results. The Triggers and Order of Execution documentation explains that e.g. workflow field updates cause the triggers to fire again and if the boolean has already been set the trigger won't fire again in cases where it should.)

In your case this could probably be accomplished by only doing the upsert payables for records that have changed i.e. where the amountToSend is different to the existing payment_to_send__c. (You may be changing other fields too and if so would need to consider those - hard to tell.)


It looks like you are having an issue with Recursive Triggers, usually when you have the Maximum Trigger Depth error that means that the trigger is getting recursively fired. You can only recursively fire 16 times.

I would take a look at the following post to help you solve this issue.

How to control recrusive triggers with static variables.

  • 1
    If you think it's a duplicate, flagging may be more appropriate than posting an answer in this case. Otherwise, adding detail from the link is recommended. The answers on that post are a poor example... – Adrian Larson Sep 2 '16 at 13:41
public class recusrssionPreventController
    public static boolean flag = true;
    public recusrssionPreventController()

Save this apex class.

Add this line in the trigger code:

if( recusrssionPreventController.flag == true){
    recusrssionPreventController.flag = false;

At last line close this bracket in your trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.