You need to sign in to do that
Don't have an account?
vinni 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);
}
}
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);
}
}
Can you try the below trigger and handler.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
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,
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
The trigger can be as simple as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
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
Do you mean when the Amount is not null and updated then only this shluld happen?
Thanks,
Thank you
Can you try the below trigger and handler.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,