0

am getting the following error:

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Effectivefilingdate: maximum trigger depth exceeded Patent trigger event AfterUpdate

below is my code-what is the change i want to do...

trigger Effectivefilingdate on Patent__c (after insert,after update) {
if(trigger.isAfter)
if(trigger.isinsert || trigger.isupdate)
{
    List<Patent__c> objPatsToUpdate = NEW List<Patent__c>();
    Set<Id> PatIds = new Set<Id>();
    for (Patent__c Pat : Trigger.new){
    PatIds.add(Pat.id);
    }
    for (Patent__c objPatent : [select Id,Effective_Filing_Date__c,Application_Date__c,Priority_Dates__c from Patent__c where Id IN:PatIds]) 
    {
        //Patent__c oldpat = Trigger.oldMap.get(objPatent.Id);

        if (objPatent.Priority_Dates__c == Null)
        {
            objPatent.Effective_Filing_Date__c = objPatent.Application_Date__c;
        }
        if (objPatent.Priority_Dates__c !=null)
        {
            List<date> lstDtPrioirtyDates = New List<date>();
            List<date> lstDtPrioirtyDates1 = New List<date>();
            List<String> lstStrPriorityDates = objPatent.Priority_Dates__c.replaceAll( '\\s+', '').split(',');
            date mydate;
            system.debug(lstStrPriorityDates);
            for(string st : lstStrPriorityDates)
            {
                date dt = date.valueof(st);
                if(mydate == null)
                {
                    mydate = dt;
                }
                else{
                if(mydate > dt)
                    mydate=dt;
                }
            }
            objPatent.Effective_Filing_Date__c = mydate;
            system.debug('Small date'+mydate);
        }
        objPatsToUpdate.add(objPatent);
    }
    if(!objPatsToUpdate.isEmpty())
    {
    update objPatsToUpdate;
    } 
 }}
1

Your trigger is recursing because, in an after update trigger, you perform a DML update on the same objects received by the trigger without altering the fields that cause your trigger to process those objects.

You make changes to Patent__c objects under two conditions, both based on Priority_Dates__c. However, your code doesn't change Priority_Dates__c when it performs an update. This ensures that your after update trigger, when it runs during the new update DML you fire, will process those same records again, and update them again, until you hit the stack depth limit.

It appears to me that your trigger could be run before insert and before update rather than after, when you can directly make changes to the objects in Trigger.New rather than querying and firing another DML update. This would prevent recursion without having to implement a more complex recursion-prevention architecture.

| improve this answer | |
  • before insert ,update also getting the same error!! – velu Apr 26 '18 at 4:41
  • Are you performing an update DML operation? – David Reed Apr 26 '18 at 10:16

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.