I tried to check previous answers here for similar issue but I could not apply it in my condition
We have a scenario where Account is master and Annual Potential is a child object,
THe scenario is for each account there should be only one active record for each quarter for each account.
There is a picklist field Year_Quarter__c, values Q1,Q2,Q3,Q4 so in the below code Iam checking the old records with new records AcctID and if the new records year quarter is same as the querried old records then the old records Active_c checkbox is made false.
trigger AnnualPotentialQuarterTrigger on Annual_Potential__c (before insert,after insert,after update) {
for(Annual_Potential__c apq : trigger.new){
if(trigger.isbefore){
apq.Annual_Potential_Name__c = apq.CC_Vehicle_Type__c+' '+apq.CC_Year__c+' '+apq.Year_Quarter__c;
}
if(trigger.isafter){
if(apq.Active__c == True){
List<Annual_Potential__c> oldRecords = [Select Id,Year_Quarter__c,Active__c From Annual_Potential__c where Id !=: apq.Id and CC_Account__c =: apq.CC_Account__c ];
system.debug(oldRecords);
if(oldRecords.size() !=0 ){
system.debug(oldRecords.size());
for(Annual_Potential__c aps:oldRecords){
if(aps.Year_Quarter__c == apq.Year_Quarter__c){
aps.Active__c = False;
}
}try{
update oldRecords;
}Catch(Exception exp){
System.debug(exp.getMessage());
}
}else
{
insert apq;
}
}
}
}
}
Iam getting following error: 13:23:59:774 USER_DEBUG [21]|DEBUG|Update failed. First exception on row 0 with id a7V3N0000004S4dUAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AnnualPotentialQuarterTrigger: maximum trigger depth exceeded
I tried to check previous answers here but I could not apply it in my condition.
Please advise.