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
vinni shreevinni shree 

execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0065i000007Y3jDAAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Usecase10: maximum trigger depth exceeded Opportunity trigger event AfterUpdate

Hi, I have written a code which is trigger that works whenever amount in opportunity is updated or inserted it should be 10% less than the actual entered value. I'm getting this error please help why this error is showing up. Thanks in advance Apex code
 
public class Usecase10 {    
 public static void invoke(list <opportunity> opportunities){         
Map<id, Opportunity> oppAmountUpdate= new Map<Id, opportunity>(); 
Map<Id, Double> oppmap= new Map<Id, Double>(); 
for(opportunity o:[select id, Amount from opportunity]){     
if(o.Amount!=null){     
o.Amount=o.amount-(o.amount*0.1);         
oppmap.put((id)o.get('Id'),(Double)o.get('Amount')); 


for(Id ids:oppmap.keySet()){     
Opportunity opp=new opportunity();     
opp.id=ids;     
opp.Amount=oppmap.get(ids);     
oppAmountUpdate.put(opp.id,opp); 

update oppAmountUpdate.values();     
}  
}
 
Trigger 
 
rigger Usecase10 on Opportunity (after insert, after update) {    
 public static boolean recursive=false;     if(!recursive){        
 recursive=true;       
Usecase10.invoke(Trigger.new); 

}
Best Answer chosen by vinni shree
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Can you try the below trigger and handler.
 
trigger Usecase10 on Opportunity (before insert, before update) {
      
    if(Trigger.isinsert)
Usecase10.invoke(Trigger.new); 
    
    if(Trigger.isupdate)
        
Usecase10.invoke1(Trigger.new,Trigger.oldmap);
}
 
public class Usecase10 {    
 public static void invoke(list <opportunity> opportunities){         
     for(opportunity opp:opportunities){
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
    public static void invoke1(list <opportunity> opportunities,map<id,opportunity> oldmap){         
     for(opportunity opp:opportunities){
         opportunity oldopp=oldmap.get(opp.id);
         if(opp.Amount!=oldopp.Amount)
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
hi Vinni,

Can I know why you are trying to query all the opportunities in apex class. Do you want to update the amount on the record which is created /Updated right?

Thanks,
 
vinni shreevinni shree

hi praveen,

 I used trigger to get new opportunities when user creates or updates and I iterated opportunities of new records only and trying to update the amount according to condition (amount should be 10% less than the actual amount entered by the user)

 

Thank you

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

The trigger can be as simple as below.
 
trigger Usecase10 on Opportunity (before insert, before update) {
      
    
Usecase10.invoke(Trigger.new); 

}
 
public class Usecase10 {    
 public static void invoke(list <opportunity> opportunities){         
     for(opportunity opp:opportunities){
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
vinni shreevinni shree

Hi praveen,

This is working but what if i need to allow this to happen only when opportunity amount is entered or updated not the opportunity(if any other fields are updated)

 

Thank you 
 

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Do you mean when the Amount is not null and updated then only this shluld happen?

Thanks,
 
vinni shreevinni shree
Yes praveen  because when I tried the logic which you sent it worked fine when i updated amount field later i updated closed date field and amount got reduced again so i dont want this to happen it should happen only when amount field is updated and not other fields 

Thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Can you try the below trigger and handler.
 
trigger Usecase10 on Opportunity (before insert, before update) {
      
    if(Trigger.isinsert)
Usecase10.invoke(Trigger.new); 
    
    if(Trigger.isupdate)
        
Usecase10.invoke1(Trigger.new,Trigger.oldmap);
}
 
public class Usecase10 {    
 public static void invoke(list <opportunity> opportunities){         
     for(opportunity opp:opportunities){
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
    public static void invoke1(list <opportunity> opportunities,map<id,opportunity> oldmap){         
     for(opportunity opp:opportunities){
         opportunity oldopp=oldmap.get(opp.id);
         if(opp.Amount!=oldopp.Amount)
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
vinni shreevinni shree
Thank you so much praveen it worked.