0

Trigger on contact It is giving the error ,please help me on this

Error:Apex trigger changestoProspectpendingEnrollment caused an unexpected exception, contact your administrator: changestoProspectpendingEnrollment: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0hc0000006qTwsAAE; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 003c000000YHLFG) is currently in trigger changestoProspectpendingEnrollment, therefore it cannot recursively update itself: []: Trigger.changestoProspectpendingEnrollment: line 66, column 1

trigger  changestoProspectpendingEnrollment on Contact (before update) {     

    for(contact con:trigger.new){

        if(con.Amount_MSRP__c != null && con.Payment_Plan__c != '' && con.RecordTypeId =='012E0000000R6UM' && con.Disposition_Code__c != ''){
            List<Transaction_Queue__c> trq =[select id,Status__c,Amount__c from Transaction_Queue__c where
                                             contact__c =:con.Id and Status__c ='Set to be Processed'];
            if(con.Charge_One_Time__c == true){ 
                if(con.Payment_Plan__c == 'Full pay' &&  con.Charge_One_Time_Amount__c == null){
                    con.Charge_One_Time_Amount__c = con.Amount_MSRP__c;
                }
                if(con.Payment_Plan__c == 'Full pay' &&  con.Charge_One_Time_Amount__c != null){
                    if(con.Charge_One_Time_Amount__c < con.Amount_MSRP__c ){
                        Trigger.new[0].Charge_One_Time_Amount__c.addError('Please Enter the amount not less than the scheduled amount');
                    }   
                }

                if(con.Payment_Plan__c == 'Semi-Annual' &&  con.Charge_One_Time_Amount__c == null){
                    con.Charge_One_Time_Amount__c = con.Amount_MSRP__c/2;  
                }
                if(con.Payment_Plan__c == 'Semi-Annual' &&  con.Charge_One_Time_Amount__c != null){
                    if(con.Charge_One_Time_Amount__c < con.Amount_MSRP__c/2 ){
                        Trigger.new[0].Charge_One_Time_Amount__c.addError('Please Enter the amount not less than the scheduled amount');
                    }
                }
                if(con.Payment_Plan__c == 'Quarterly' &&  con.Charge_One_Time_Amount__c == null){
                    con.Charge_One_Time_Amount__c = con.Amount_MSRP__c/4;  
                }
                if(con.Payment_Plan__c == 'Quarterly' &&  con.Charge_One_Time_Amount__c != null){
                    if(con.Charge_One_Time_Amount__c < con.Amount_MSRP__c/4 ){
                        Trigger.new[0].Charge_One_Time_Amount__c.addError('Please Enter the amount not less than the scheduled amount');
                    }  
                }
                if(con.Payment_Plan__c == 'Monthly' &&  con.Charge_One_Time_Amount__c == null){
                    con.Charge_One_Time_Amount__c = con.Amount_MSRP__c/12;  
                }
                if(con.Payment_Plan__c == 'Monthly' &&  con.Charge_One_Time_Amount__c != null){
                    if(con.Charge_One_Time_Amount__c < con.Amount_MSRP__c/12 ){
                        Trigger.new[0].Charge_One_Time_Amount__c.addError('Please Enter the amount not less than the scheduled amount');
                    } 
                }

                if(trq.isEmpty()==false){
                    for(Transaction_Queue__c t : trq){
                        t.Amount__c=math.abs(con.Charge_One_Time_Amount__c);
                    }
                    update trq;
                }

                if(trq.isEmpty()==true){
                    PayPageUtil.createTransactionque(con.Id, con.Default_Payment_Profile__c, 'Set to be Processed', Math.abs(con.Charge_One_Time_Amount__c));
                }
                con.AutoPay__c = false;
                con.Print_Invoice__c = true;

            }

            if(con.Charge_One_Time__c == false){
                con.Charge_One_Time_Amount__c = null;
                con.AutoPay__c = true;
                con.Print_Invoice__c = false; 
                if(trq.isEmpty()==false){
                    for(Transaction_Queue__c t : trq){
                        t.Status__c = 'Cancelled';
                    }
                    update trq;
                    break;
                }   

            }

        }
    } 
}
0

It seems like you are trying to update the same contact which has not yet been updated in the above trigger context. This might possibly be due to an apex trigger/workflow on Transaction_Queue__c object that tries to update related contact field.

So you need to ensure that both these actions happen in different transaction, also as I best practice whenever I perform DMLs , I go for after triggers and not before.

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.