0

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.

2 Answers 2

1

Hi I have found the solution for the above issue,

I have handled the recursion by trigger handler class.

Hope this will be helpful for others in future.

Trigger:

trigger AnnualPotentialQuarterTrigger on Annual_Potential__c (before insert,after insert,after update) {

for(Annual_Potential__c aps : trigger.new){
    if(Trigger.isbefore){
      aps.Annual_Potential_Name__c = aps.CC_Vehicle_Type__c+' '+aps.CC_Year__c+' '+aps.Year_Quarter__c;  
    }
}

if(AccountPotentialTriggerHandler.isRecursive == false){ AccountPotentialTriggerHandler.isRecursive = true; AccountPotentialTriggerHandler.UpdateAccountPotentials(trigger.new); }
}

Trigger Handler class:

public class AccountPotentialTriggerHandler {

public static boolean isRecursive = false;

public static void UpdateAccountPotentials(List<Annual_Potential__c> newAnnualPotentialRecords){

Set<Id> AccountIdSet = new Set<Id>();
    for(Annual_Potential__c ap:newAnnualPotentialRecords)
    AccountIdSet.add(ap.CC_Account__c);

List<Annual_Potential__c> OldAccountPotentials = [select Id, Year_Quarter__c, Active__c, CC_Account__c from Annual_Potential__c where Active__c=true and CC_Account__c in : AccountIdSet];
Map<Id, List<Annual_Potential__c>> MapOfAccountIdAndListAccountPotential = new Map<Id, List<Annual_Potential__c>>();
for(Annual_Potential__c ap:OldAccountPotentials){
    if(!MapOfAccountIdAndListAccountPotential.containsKey(ap.CC_Account__c)){
        List<Annual_Potential__c> tempList = new List<Annual_Potential__c>();
        tempList.add(ap);
        MapOfAccountIdAndListAccountPotential.put(ap.CC_Account__c, tempList);
    }else{
       MapOfAccountIdAndListAccountPotential.get(ap.CC_Account__c).add(ap); 
    }
}

List<Annual_Potential__c> FinalUpdateList = new List<Annual_Potential__c>();
if(MapOfAccountIdAndListAccountPotential.size()>0)
for(Annual_Potential__c ap:newAnnualPotentialRecords){
    for(Annual_Potential__c temp:MapOfAccountIdAndListAccountPotential.get(ap.CC_Account__c)){
        if(temp.Id != ap.Id && ap.Year_Quarter__c == temp.Year_Quarter__c && ap.Active__c == temp.Active__c && ap.Active__c == True){
            temp.Active__c = false;
            FinalUpdateList.add(temp);
        }
    }
}
System.debug('FinalUpdateList '+FinalUpdateList.size());
try{
    if(FinalUpdateList.size()>0){
        update FinalUpdateList;
    }

}catch(Exception exp){
    System.debug(exp.getMessage());
}

}

}

Thanks

0

Apex triggers doesnt have to update many record feature you are using loop,There is can too many record have there. For this situation you can use batch apex with workflow or you can add in your trigger,

I wrote sample batch apex;

global class AnnualPotentialQuarterBatchProcess implements Database.Batchable<sObject> {

global  String query;
global  Id ApqId;
global  Id AccountCC;
global  Integer ApqYearQuarter;

// I dont know your variables features, you can edit.
public static void AnnualPotentialQuarterBatchProcess(Id ApqId, Id AccountCC, Integer ApqYearQuarter ,String PotentialName) {
    oldRecords = oldRecords;
    ApqId = ApqId;
    AccountCC = AccountCC;
    ApqYearQuarter = ApqYearQuarter;
}

global Database.QueryLocator start(Database.BatchableContext info){
    //OldRecords.
    return Database.getQueryLocator([Select Id,Year_Quarter__c,Active__c From Annual_Potential__c where Id !=: apqId AND CC_Account__c =: AccountCC]);
}

global void execute(Database.BatchableContext BC, List<Annual_Potential__c> scope){

    for(Annual_Potential__c oldrecord: scope) {
        if (oldrecord.Year_Quarter__c == ApqYearQuarter) {
            oldrecord.Active__c = false;
        }
    }
    try {
        update oldRecords;
    }catch (Exception e) {
        System.debug(e.getMessage());
    }
}

    global void finish(Database.BatchableContext BC){}
}

And Trigger Process,

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) {
                ID jobid = Database.executeBatch(new AnnualPotentialQuarterBatchProcess(apq.Id, apq.CC_Account__c, Apq.YearQarter, apq.Annual_Potential_Name__c), 10);
            } else {
                insert apq;
               }
           }
       }
    }
}

Hopefully its can be work for you,

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .