0

I believe I'm having some issues with recursion. My Opportunity trigger creates an engagement object whenever the stage attribute reaches a predetermined value. When the engagement is created I want to share its Id with the parent opportunity. A related engagement Id will show on opportunity update or if I create the opportunity with the predefined value to create an engagement, but not immediately if I update an opportunity to the predefined value. My code and the debug log are below. Any help is appreciated.

trigger EngagementTriggers on Engagement__c (after insert, before update, before delete) 
{
    EngagementTriggersClass trigs=new EngagementTriggersClass();
    trigs.executeEng(trigger.new, trigger.oldmap);    
}

Class

public class EngagementTriggersClass 
{
     public void executeEng(List<Engagement__c> newList, Map<ID, Engagement__c> oldMap)
   {
        if(trigger.isinsert)
        {               
            List<Engagement__c> newEngagements=[select id, Opportunity_ID__c from Engagement__c where id in:trigger.newmap.keyset()];           
            List<opportunity>forUpdate=new List<opportunity>();

            Map<Id, Engagement__c> engMap=new map<Id, Engagement__c>();
            for(Engagement__c eng:newEngagements)
            {
                engmap.put(eng.opportunity_id__c, eng);
                Opportunity newOpp=[select id from opportunity where  id=:eng.Opportunity_ID__c];
                forupdate.add(newopp);
            }

            for(opportunity opps:forUpdate)
            {
                for(Engagement__c engs:newEngagements)
                {
                    opps.Related_Engagement__c=engs.id;
                    update opps;
                }
            }                       
        }
    }
}

Trigger

trigger OpportunityTriggers on Opportunity (after insert, before update, before delete) 
{
    OpportunityTriggersClass trigs=new OpportunityTriggersClass();
    trigs.executeopp(trigger.new, trigger.oldmap);
}

Class

public class OpportunityTriggersClass 
{   
    public void ExecuteOpp(List<Opportunity> newList, Map<ID, Opportunity> oldMap)
    {

        if(trigger.isinsert)
        {
            List<Engagement__c> forInsert=new List<Engagement__c>();  
            for(opportunity opp:newList)
            {          

                if(opp.StageName==opp.Set_Value_to_create_Engagement__c)
                {

                    Engagement__c newengagement=new Engagement__c();
                    newengagement.name=opp.Name;
                    newengagement.opportunity_id__c=opp.Id;  
                    newengagement.Stage__c=opp.StageName;
                   // opp.Related_Engagement__c=newengagement.id; engagement isn't inserted yet. 
                    forInsert.add(newengagement);           
                }
            }
            if(forInsert.size() > 0)
            {
                try
                {                                                        
                    insert forInsert;
                }
                catch(DMLException e)
                {
                    // error handling goes here
                }
            }
        }
        if(trigger.isupdate)
        {
            List<Engagement__c> forUpdate=new List<Engagement__c>();
            List<Engagement__c> forInsert=new List<Engagement__c>();
            List<engagement__c> EngagementsWithUpdatedParents=[select id, opportunity_id__c from engagement__c where opportunity_id__c in :oldmap.keySet()];
            for(opportunity opp:newList)
            {       
                if(engagementswithupdatedparents.size()>0) 
                {
                    for(engagement__c eng:EngagementsWithUpdatedParents)
                    {                       
                       if(eng.Opportunity_ID__c ==opp.ID)
                        {                                                                                                                                                                                                                                      
                            eng.Stage__c=opp.StageName;
                            opp.Related_Engagement__c=eng.id;                                                       
                            Forupdate.add(eng);                                                     
                        }

                        else
                        {

                        }
                    }               
                }      
                else   //create an engagement if it matches the create engagement value
                {
                    if(opp.StageName==opp.Set_Value_to_create_Engagement__c )
                    {
                        Engagement__c newengagement=new Engagement__c();
                        newengagement.name=opp.Name;
                        newengagement.opportunity_id__c=opp.Id;
                        newengagement.Stage__c=opp.StageName;
                        opp.Related_Engagement__c=newengagement.id;
                        Forinsert.add(newengagement);
                    } 
                }
            }
            if(forUpdate.size() > 0||forInsert.size()>0)
            {
                try
                {
                update forUpdate;
                insert forInsert;

                }
                catch(DMLException e)
                {
                    // error handling goes here

                }
            }       
        }
        if(trigger.isdelete)
        {
            List<opportunity> OpptysDelete=[select id from opportunity where id in :oldmap.keyset()];
            List<engagement__c> allEngagements=[select id, opportunity_id__c from engagement__c];
                for(engagement__c eng: allEngagements) 
                {
                    for (Opportunity o : OpptysDelete)
                    { 
                        if(o.Id==eng.Opportunity_ID__c)
                        { 
                            Opportunity OpptyWithChild = oldMap.get(o.Id); 
                            OpptyWithChild.adderror('You cannot delete this Opportunity as it has one or more Engagements associated with it'); 
                        } 
                    } 
                }       
        }
    }
}

Debug Log

34.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 15:34:16.056 (56574119)|EXECUTION_STARTED 15:34:16.056 (56612649)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS 15:34:16.056 (56656949)|CODE_UNIT_STARTED|[EXTERNAL]|01q1a000000Qo7A|OpportunityTriggers on Opportunity trigger event BeforeUpdate for [0061a00000BMG3h] 15:34:16.056 (56730618)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.056 (60308412)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.060 (60339488)|VARIABLE_SCOPE_BEGIN|[1]|this|OpportunityTriggers|true|false 15:34:16.060 (60405210)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x403c5161 15:34:16.060 (60467032)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.060 (60482479)|VARIABLE_SCOPE_BEGIN|[1]|this|OpportunityTriggers|true|false 15:34:16.060 (60501627)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x403c5161 15:34:16.060 (60510426)|STATEMENT_EXECUTE|[1] 15:34:16.060 (60514110)|STATEMENT_EXECUTE|[3] 15:34:16.060 (60520533)|HEAP_ALLOCATE|[3]|Bytes:1 15:34:16.060 (60525832)|HEAP_ALLOCATE|[3]|Bytes:17 15:34:16.060 (60563162)|METHOD_ENTRY|[1]|01p1a000002LGVU|OpportunityTriggersClass.OpportunityTriggersClass() 15:34:16.060 (60569216)|STATEMENT_EXECUTE|[1] 15:34:16.060 (60573143)|STATEMENT_EXECUTE|[1] 15:34:16.060 (60579226)|METHOD_EXIT|[1]|OpportunityTriggersClass 15:34:16.060 (60596094)|HEAP_ALLOCATE|[3]|Bytes:4 15:34:16.060 (60613216)|CONSTRUCTOR_ENTRY|[3]|01p1a000002LGVU|() 15:34:16.060 (60649716)|VARIABLE_SCOPE_BEGIN|[1]|this|OpportunityTriggersClass|true|false 15:34:16.060 (60669691)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x7ec1162f 15:34:16.060 (60683674)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:6 15:34:16.060 (60696603)|VARIABLE_SCOPE_BEGIN|[2]|this|System.ApexBaseClass|true|false 15:34:16.060 (60711607)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x7ec1162f 15:34:16.060 (60723647)|STATEMENT_EXECUTE|[1] 15:34:16.060 (60730566)|CONSTRUCTOR_EXIT|[3]|01p1a000002LGVU|() 15:34:16.060 (60751725)|HEAP_ALLOCATE|[50]|Bytes:5 15:34:16.060 (60770495)|HEAP_ALLOCATE|[56]|Bytes:5 15:34:16.060 (60781356)|HEAP_ALLOCATE|[63]|Bytes:7 15:34:16.060 (60818578)|VARIABLE_ASSIGNMENT|[3]|this.trigs|0x7ec1162f|0x403c5161 15:34:16.060 (60823476)|STATEMENT_EXECUTE|[4] 15:34:16.061 (61642527)|METHOD_ENTRY|[4]|01p1a000002LGVU|OpportunityTriggersClass.ExecuteOpp(List, Map) 15:34:16.061 (61673318)|VARIABLE_SCOPE_BEGIN|[3]|this|OpportunityTriggersClass|true|false 15:34:16.061 (61693156)|VARIABLE_ASSIGNMENT|[3]|this|{}|0x7ec1162f 15:34:16.061 (61700409)|VARIABLE_SCOPE_BEGIN|[3]|newList|List|true|false 15:34:16.062 (62275498)|VARIABLE_ASSIGNMENT|[3]|newList|{"s":1,"v":[{"ForecastCategory":"Pipeline","StageName":"Needs Analysis","Probability":20,"IsWon":false,"LastModifiedById":"0051a000000T4HOAA0","OwnerId":"0051a000000T4HOAA0","LastModifiedDate":"2015-08-24T19:34:10.000Z","IsPrivate":false,"Name":"gogo7070","IsClosed":false,"HasOpportunityLineIt (2 more) ...":false,"SystemModstamp":"2015-08-24T19:34:10.000Z","CreatedById":"0051a000000T4HOAA0","CreatedDate":"2015-08-24T19:34:10.000Z","Set_Value_to_create_ (13 more) ...":"Needs Analysis","IsDeleted":false,"IsExcludedFromTerrit (10 more) ...":false,"ForecastCategoryName":"Pipeline","Id":"0061a00000BMG3hAAH","CloseDate":"2015-09-03T00:00:00.000Z"}]}|0x1ec2d034 15:34:16.062 (62290544)|VARIABLE_SCOPE_BEGIN|[3]|oldMap|Map|true|false 15:34:16.062 (62630899)|VARIABLE_ASSIGNMENT|[3]|oldMap|{"s":1,"v":{"0061a00000BMG3hAAH":{"ForecastCategory":"Pipeline","StageName":"Qualification","Probability":10,"IsWon":false,"LastModifiedById":"0051a000000T4HOAA0","OwnerId":"0051a000000T4HOAA0","LastModifiedDate":"2015-08-24T19:34:10.000Z","IsPrivate":false,"Name":"gogo7070","IsClosed":false,"HasOpportunityLineIt (2 more) ...":false,"SystemModstamp":"2015-08-24T19:34:10.000Z","CreatedById":"0051a000000T4HOAA0","CreatedDate":"2015-08-24T19:34:10.000Z","Set_Value_to_create_ (13 more) ...":"Needs Analysis","IsDeleted":false,"IsExcludedFromTerrit (10 more) ...":false,"ForecastCategoryName":"Pipeline","Id":"0061a00000BMG3hAAH","CloseDate":"2015-09-03T00:00:00.000Z"}}}|0x5ba418b4 (62725855)|HEAP_ALLOCATE|[37]|Bytes:4 15:34:16.062 (62793660)|SYSTEM_CONSTRUCTOR_ENTRY|[37]|() 15:34:16.062 (62832085)|SYSTEM_CONSTRUCTOR_EXIT|[37]|() 15:34:16.062 (62839093)|VARIABLE_SCOPE_BEGIN|[37]|forUpdate|List|true|false 15:34:16.062 (62848494)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 15:34:16.062 (62863825)|VARIABLE_ASSIGNMENT|[37]|forUpdate|{"s":1,"v":[]}|0x7252006c 15:34:16.062 (62869268)|STATEMENT_EXECUTE|[38] 15:34:16.062 (62874689)|HEAP_ALLOCATE|[38]|Bytes:4 15:34:16.062 (62881904)|SYSTEM_CONSTRUCTOR_ENTRY|[38]|() 15:34:16.062 (62895816)|SYSTEM_CONSTRUCTOR_EXIT|[38]|() 15:34:16.062 (62900085)|VARIABLE_SCOPE_BEGIN|[38]|forInsert|List|true|false 15:34:16.062 (62905805)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 15:34:16.062 (62917938)|VARIABLE_ASSIGNMENT|[38]|forInsert|{"s":1,"v":[]}|0x6599ed16 15:34:16.062 (62922826)|STATEMENT_EXECUTE|[39] 15:34:16.062 (62927294)|HEAP_ALLOCATE|[39]|Bytes:82 15:34:16.062 (62940883)|HEAP_ALLOCATE|[39]|Bytes:4 15:34:16.062 (62951686)|HEAP_ALLOCATE|[39]|Bytes:7 15:34:16.063 (63001697)|SYSTEM_METHOD_ENTRY|[39]|Map.keySet() 15:34:16.063 (63063445)|HEAP_ALLOCATE|[39]|Bytes:26 15:34:16.063 (63074207)|SYSTEM_METHOD_EXIT|[39]|Map.keySet() 15:34:16.064 (64475888)|SOQL_EXECUTE_BEGIN|[39]|Aggregations:0|SELECT id, opportunity_id__c FROM engagement__c WHERE opportunity_id__c = :tmpVar1 15:34:16.066 (66714004)|SOQL_EXECUTE_END|[39]|Rows:0 15:34:16.066 (66733503)|HEAP_ALLOCATE|[39]|Bytes:4 15:34:16.066 (66745699)|HEAP_ALLOCATE|[39]|Bytes:0 15:34:16.066 (66767445)|HEAP_ALLOCATE|[39]|Bytes:4 15:34:16.066 (66776093)|VARIABLE_SCOPE_BEGIN|[39]|EngagementsWithUpdatedParents|List|true|false 15:34:16.066 (66794833)|VARIABLE_ASSIGNMENT|[39]|EngagementsWithUpdatedParents|{"s":1,"v":[]}|0x3e1942de 15:34:16.066 (66842053)|SYSTEM_METHOD_ENTRY|[40]|List.iterator() 15:34:16.067 (67032066)|SYSTEM_METHOD_EXIT|[40]|List.iterator() 15:34:16.067 (67059251)|SYSTEM_METHOD_ENTRY|[40]|system.ListIterator.hasNext() 15:34:16.067 (67085421)|HEAP_ALLOCATE|[40]|Bytes:5 15:34:16.067 (67094398)|SYSTEM_METHOD_EXIT|[40]|system.ListIterator.hasNext() 15:34:16.067 (67116724)|HEAP_ALLOCATE|[40]|Bytes:4 15:34:16.067 (67129568)|VARIABLE_SCOPE_BEGIN|[40]|opp|Opportunity|true|false 15:34:16.067 (67366757)|VARIABLE_ASSIGNMENT|[40]|opp|{"ForecastCategory":"Pipeline","StageName":"Needs Analysis","Probability":20,"IsWon":false,"LastModifiedById":"0051a000000T4HOAA0","OwnerId":"0051a000000T4HOAA0","LastModifiedDate":"2015-08-24T19:34:10.000Z","IsPrivate":false,"Name":"gogo7070","IsClosed":false,"HasOpportunityLineIt (2 more) ...":false,"SystemModstamp":"2015-08-24T19:34:10.000Z","CreatedById":"0051a000000T4HOAA0","CreatedDate":"2015-08-24T19:34:10.000Z","Set_Value_to_create_ (13 more) ...":"Needs Analysis","IsDeleted":false,"IsExcludedFromTerrit (10 more) ...":false,"ForecastCategoryName":"Pipeline","Id":"0061a00000BMG3hAAH","CloseDate":"2015-09-03T00:00:00.000Z"}|0x6c751cab 15:34:16.067 (67379348)|STATEMENT_EXECUTE|[41] 15:34:16.067 (67393467)|SYSTEM_METHOD_ENTRY|[42]|List.size() 15:34:16.067 (67411776)|SYSTEM_METHOD_EXIT|[42]|List.size() 15:34:16.067 (67422136)|STATEMENT_EXECUTE|[59] 15:34:16.067 (67424666)|STATEMENT_EXECUTE|[59] 15:34:16.067 (67428848)|HEAP_ALLOCATE|[59]|Bytes:1 15:34:16.067 (67448997)|SYSTEM_METHOD_ENTRY|[59]|System.debug(ANY) 15:34:16.067 (67470978)|USER_DEBUG|[59]|DEBUG|e 15:34:16.067 (67477931)|SYSTEM_METHOD_EXIT|[59]|System.debug(ANY) 15:34:16.067 (67532823)|STATEMENT_EXECUTE|[61] 15:34:16.067 (67536710)|STATEMENT_EXECUTE|[61] 15:34:16.067 (67540117)|HEAP_ALLOCATE|[61]|Bytes:1 15:34:16.067 (67549292)|SYSTEM_METHOD_ENTRY|[61]|System.debug(ANY) 15:34:16.067 (67563398)|USER_DEBUG|[61]|DEBUG|f 15:34:16.067 (67569119)|SYSTEM_METHOD_EXIT|[61]|System.debug(ANY) 15:34:16.067 (67573317)|STATEMENT_EXECUTE|[62] 15:34:16.067 (67594335)|HEAP_ALLOCATE|[62]|Bytes:4 15:34:16.067 (67699395)|VARIABLE_SCOPE_BEGIN|[62]|newengagement|Engagement__c|true|false 15:34:16.067 (67716879)|VARIABLE_ASSIGNMENT|[62]|newengagement|{}|0x7cad562d 15:34:16.067 (67721924)|STATEMENT_EXECUTE|[62] 15:34:16.067 (67726212)|HEAP_ALLOCATE|[62]|Bytes:1 15:34:16.067 (67737084)|SYSTEM_METHOD_ENTRY|[62]|System.debug(ANY) 15:34:16.067 (67751811)|USER_DEBUG|[62]|DEBUG|g 15:34:16.067 (67757737)|SYSTEM_METHOD_EXIT|[62]|System.debug(ANY) 15:34:16.067 (67762198)|STATEMENT_EXECUTE|[63] 15:34:16.067 (67832028)|VARIABLE_ASSIGNMENT|[63]|this.Name|"gogo7070"|0x7cad562d 15:34:16.067 (67838110)|STATEMENT_EXECUTE|[64] 15:34:16.067 (67874826)|HEAP_ALLOCATE|[64]|Bytes:18 15:34:16.067 (67916094)|VARIABLE_ASSIGNMENT|[64]|this.Opportunity_ID__c|"0061a00000BMG3hAAH"|0x7cad562d 15:34:16.067 (67921710)|STATEMENT_EXECUTE|[65] 15:34:16.067 (67954366)|VARIABLE_ASSIGNMENT|[65]|this.Stage__c|"Needs Analysis"|0x7cad562d 15:34:16.067 (67959517)|STATEMENT_EXECUTE|[66] 15:34:16.068 (68174308)|VARIABLE_ASSIGNMENT|[66]|this.Related_Engagement__c|null|0x6c751cab 15:34:16.068 (68182700)|STATEMENT_EXECUTE|[67] 15:34:16.068 (68201671)|SYSTEM_METHOD_ENTRY|[67]|List.add(Object) 15:34:16.068 (68220714)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 15:34:16.068 (68231691)|SYSTEM_METHOD_EXIT|[67]|List.add(Object) 15:34:16.068 (68241001)|SYSTEM_METHOD_ENTRY|[40]|system.ListIterator.hasNext() 15:34:16.068 (68251519)|HEAP_ALLOCATE|[40]|Bytes:5 15:34:16.068 (68258223)|SYSTEM_METHOD_EXIT|[40]|system.ListIterator.hasNext() 15:34:16.068 (68270477)|VARIABLE_ASSIGNMENT|[40]|opp|null| 15:34:16.068 (68279984)|SYSTEM_METHOD_ENTRY|[71]|List.size() 15:34:16.068 (68293653)|SYSTEM_METHOD_EXIT|[71]|List.size() 15:34:16.068 (68302223)|SYSTEM_METHOD_ENTRY|[71]|List.size() 15:34:16.068 (68307662)|SYSTEM_METHOD_EXIT|[71]|List.size() 15:34:16.068 (68315951)|STATEMENT_EXECUTE|[72] 15:34:16.068 (68318645)|STATEMENT_EXECUTE|[73] 15:34:16.068 (68320572)|STATEMENT_EXECUTE|[74] 15:34:16.068 (68322515)|STATEMENT_EXECUTE|[75] 15:34:16.068 (68412685)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 15:34:16.068 (68420770)|STATEMENT_EXECUTE|[75] 15:34:16.068 (68425294)|HEAP_ALLOCATE|[75]|Bytes:1 15:34:16.068 (68435865)|SYSTEM_METHOD_ENTRY|[75]|System.debug(ANY) 15:34:16.068 (68441972)|USER_DEBUG|[75]|DEBUG|h 15:34:16.068 (68446713)|SYSTEM_METHOD_EXIT|[75]|System.debug(ANY) 15:34:16.068 (68450588)|STATEMENT_EXECUTE|[76] 15:34:16.068 (68487166)|DML_BEGIN|[76]|Op:Insert|Type:Engagement__c|Rows:1 15:34:16.068 (68504958)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.095 (95739369)|CODE_UNIT_STARTED|[EXTERNAL]|01q1a000000Qp2X|EngagementTriggers on Engagement trigger event AfterInsert for [a1x1a000000M9Pa] 15:34:16.095 (95783364)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.097 (97635832)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.097 (97658179)|VARIABLE_SCOPE_BEGIN|[1]|this|EngagementTriggers|true|false 15:34:16.097 (97702083)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x324c74d5 15:34:16.097 (97756822)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.097 (97771572)|VARIABLE_SCOPE_BEGIN|[1]|this|EngagementTriggers|true|false 15:34:16.097 (97790733)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x324c74d5 15:34:16.097 (97797946)|STATEMENT_EXECUTE|[1] 15:34:16.097 (97801102)|STATEMENT_EXECUTE|[3] 15:34:16.097 (97808448)|HEAP_ALLOCATE|[3]|Bytes:1 15:34:16.097 (97814745)|HEAP_ALLOCATE|[3]|Bytes:2 15:34:16.097 (97851284)|METHOD_ENTRY|[1]|01p1a000002LI8D|EngagementTriggersClass.EngagementTriggersClass() 15:34:16.097 (97856617)|STATEMENT_EXECUTE|[1] 15:34:16.097 (97860375)|STATEMENT_EXECUTE|[1] 15:34:16.097 (97865908)|METHOD_EXIT|[1]|EngagementTriggersClass 15:34:16.097 (97881709)|HEAP_ALLOCATE|[3]|Bytes:4 15:34:16.097 (97894923)|CONSTRUCTOR_ENTRY|[3]|01p1a000002LI8D|() 15:34:16.097 (97926466)|VARIABLE_SCOPE_BEGIN|[1]|this|EngagementTriggersClass|true|false 15:34:16.097 (97946467)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x49aff0c7 15:34:16.097 (97956057)|VARIABLE_SCOPE_BEGIN|[2]|this|System.ApexBaseClass|true|false 15:34:16.097 (97968291)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x49aff0c7 15:34:16.097 (97977617)|STATEMENT_EXECUTE|[1] 15:34:16.097 (97983748)|CONSTRUCTOR_EXIT|[3]|01p1a000002LI8D|() 15:34:16.098 (98002253)|VARIABLE_ASSIGNMENT|[3]|this.trigs|0x49aff0c7|0x324c74d5 15:34:16.098 (98007402)|STATEMENT_EXECUTE|[4] 15:34:16.098 (98656304)|METHOD_ENTRY|[4]|01p1a000002LI8D|EngagementTriggersClass.executeEng(List, Map) 15:34:16.098 (98686142)|VARIABLE_SCOPE_BEGIN|[3]|this|EngagementTriggersClass|true|false 15:34:16.098 (98704753)|VARIABLE_ASSIGNMENT|[3]|this|{}|0x49aff0c7 15:34:16.098 (98711613)|VARIABLE_SCOPE_BEGIN|[3]|newList|List|true|false 15:34:16.098 (98900078)|VARIABLE_ASSIGNMENT|[3]|newList|{"s":1,"v":[{"Name":"gogo7070","Opportunity_ID__c":"0061a00000BMG3hAAH","LastModifiedById":"0051a000000T4HOAA0","SystemModstamp":"2015-08-24T19:34:16.000Z","CreatedById":"0051a000000T4HOAA0","OwnerId":"0051a000000T4HOAA0","CreatedDate":"2015-08-24T19:34:16.000Z","IsDeleted":false,"LastModifiedDate":"2015-08-24T19:34:16.000Z","Id":"a1x1a000000M9PaAAK","Stage__c":"Needs Analysis"}]}|0x7b3a1384 15:34:16.098 (98912818)|VARIABLE_SCOPE_BEGIN|[3]|oldMap|Map|true|false 15:34:16.098 (98920146)|VARIABLE_ASSIGNMENT|[3]|oldMap|null| 15:34:16.098 (98926685)|STATEMENT_EXECUTE|[4] 15:34:16.098 (98935122)|STATEMENT_EXECUTE|[6] 15:34:16.098 (98937598)|STATEMENT_EXECUTE|[9] 15:34:16.098 (98942306)|HEAP_ALLOCATE|[9]|Bytes:67 15:34:16.098 (98952266)|HEAP_ALLOCATE|[9]|Bytes:4 15:34:16.099 (99013340)|SYSTEM_METHOD_ENTRY|[9]|Map.keySet() 15:34:16.099 (99069395)|HEAP_ALLOCATE|[9]|Bytes:26 15:34:16.099 (99079665)|SYSTEM_METHOD_EXIT|[9]|Map.keySet() 15:34:16.099 (99288445)|SOQL_EXECUTE_BEGIN|[9]|Aggregations:0|SELECT id, Opportunity_ID__c FROM Engagement__c WHERE id = :tmpVar1 15:34:16.101 (101231597)|SOQL_EXECUTE_END|[9]|Rows:1 15:34:16.101 (101247550)|HEAP_ALLOCATE|[9]|Bytes:8 15:34:16.101 (101264330)|HEAP_ALLOCATE|[9]|Bytes:68 15:34:16.101 (101278785)|HEAP_ALLOCATE|[9]|Bytes:8 15:34:16.101 (101287582)|VARIABLE_SCOPE_BEGIN|[9]|newEngagements|List|true|false 15:34:16.101 (101318786)|VARIABLE_ASSIGNMENT|[9]|newEngagements|{"s":1,"v":[{"s":2,"v":{"Id":"a1x1a000000M9PaAAK","Opportunity_ID__c":"0061a00000BMG3hAAH"}}]}|0x1ccf38f5 15:34:16.101 (101325403)|STATEMENT_EXECUTE|[10] 15:34:16.101 (101340197)|SYSTEM_METHOD_ENTRY|[10]|List.size() 15:34:16.101 (101359549)|SYSTEM_METHOD_EXIT|[10]|List.size() 15:34:16.101 (101374445)|SYSTEM_METHOD_ENTRY|[10]|System.debug(ANY) 15:34:16.101 (101381558)|USER_DEBUG|[10]|DEBUG|1 15:34:16.101 (101386712)|SYSTEM_METHOD_EXIT|[10]|System.debug(ANY) 15:34:16.101 (101390609)|STATEMENT_EXECUTE|[11] 15:34:16.101 (101396543)|HEAP_ALLOCATE|[11]|Bytes:4 15:34:16.101 (101406262)|SYSTEM_CONSTRUCTOR_ENTRY|[11]|() 15:34:16.101 (101420635)|SYSTEM_CONSTRUCTOR_EXIT|[11]|() 15:34:16.101 (101425623)|VARIABLE_SCOPE_BEGIN|[11]|forUpdate|List|true|false 15:34:16.101 (101432608)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 15:34:16.101 (101446600)|VARIABLE_ASSIGNMENT|[11]|forUpdate|{"s":1,"v":[]}|0x2943f7b 15:34:16.101 (101451604)|STATEMENT_EXECUTE|[13] 15:34:16.101 (101459327)|HEAP_ALLOCATE|[13]|Bytes:4 15:34:16.101 (101468509)|VARIABLE_SCOPE_BEGIN|[13]|engMap|Map|true|false 15:34:16.101 (101488716)|VARIABLE_ASSIGNMENT|[13]|engMap|{"s":1,"v":{}}|0x52d3a58f 15:34:16.101 (101500347)|SYSTEM_METHOD_ENTRY|[14]|List.iterator() 15:34:16.101 (101638752)|SYSTEM_METHOD_EXIT|[14]|List.iterator() 15:34:16.101 (101659479)|SYSTEM_METHOD_ENTRY|[14]|system.ListIterator.hasNext() 15:34:16.101 (101672686)|HEAP_ALLOCATE|[14]|Bytes:5 15:34:16.101 (101680176)|SYSTEM_METHOD_EXIT|[14]|system.ListIterator.hasNext() 15:34:16.101 (101705613)|HEAP_ALLOCATE|[14]|Bytes:12 15:34:16.101 (101718333)|VARIABLE_SCOPE_BEGIN|[14]|eng|Engagement__c|true|false 15:34:16.101 (101738524)|VARIABLE_ASSIGNMENT|[14]|eng|{"s":1,"v":{"Id":"a1x1a000000M9PaAAK","Opportunity_ID__c":"0061a00000BMG3hAAH"}}|0x68a46b99 15:34:16.101 (101744410)|STATEMENT_EXECUTE|[15] 15:34:16.101 (101746815)|STATEMENT_EXECUTE|[16] 15:34:16.101 (101791618)|SYSTEM_METHOD_ENTRY|[16]|Map.put(Object, Object) 15:34:16.101 (101827488)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:-4 15:34:16.101 (101842647)|SYSTEM_METHOD_EXIT|[16]|Map.put(Object, Object) 15:34:16.101 (101848336)|STATEMENT_EXECUTE|[17] 15:34:16.101 (101852168)|HEAP_ALLOCATE|[17]|Bytes:46 15:34:16.101 (101860855)|HEAP_ALLOCATE|[17]|Bytes:4 15:34:16.101 (101995961)|SOQL_EXECUTE_BEGIN|[17]|Aggregations:0|SELECT id FROM opportunity WHERE id = :tmpVar1 15:34:16.103 (103996401)|SOQL_EXECUTE_END|[17]|Rows:1 15:34:16.104 (104009359)|HEAP_ALLOCATE|[17]|Bytes:8 15:34:16.104 (104021529)|HEAP_ALLOCATE|[17]|Bytes:29 15:34:16.104 (104036154)|HEAP_ALLOCATE|[17]|Bytes:8 15:34:16.104 (104044808)|HEAP_ALLOCATE|[17]|Bytes:37 15:34:16.104 (104071728)|HEAP_ALLOCATE|[17]|Bytes:8 15:34:16.104 (104080303)|VARIABLE_SCOPE_BEGIN|[17]|newOpp|Opportunity|true|false 15:34:16.104 (104101512)|VARIABLE_ASSIGNMENT|[17]|newOpp|{"s":1,"v":{"Id":"0061a00000BMG3hAAH"}}|0x3c2db600 15:34:16.104 (104107091)|STATEMENT_EXECUTE|[18] 15:34:16.104 (104125756)|SYSTEM_METHOD_ENTRY|[18]|List.add(Object) 15:34:16.104 (104145012)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 15:34:16.104 (104155939)|SYSTEM_METHOD_EXIT|[18]|List.add(Object) 15:34:16.104 (104165361)|SYSTEM_METHOD_ENTRY|[14]|system.ListIterator.hasNext() 15:34:16.104 (104175720)|HEAP_ALLOCATE|[14]|Bytes:5 15:34:16.104 (104182718)|SYSTEM_METHOD_EXIT|[14]|system.ListIterator.hasNext() 15:34:16.104 (104195514)|VARIABLE_ASSIGNMENT|[14]|eng|null| 15:34:16.104 (104205865)|SYSTEM_METHOD_ENTRY|[21]|List.iterator() 15:34:16.104 (104221556)|SYSTEM_METHOD_EXIT|[21]|List.iterator() 15:34:16.104 (104230073)|SYSTEM_METHOD_ENTRY|[21]|system.ListIterator.hasNext() 15:34:16.104 (104238623)|HEAP_ALLOCATE|[21]|Bytes:5 15:34:16.104 (104244759)|SYSTEM_METHOD_EXIT|[21]|system.ListIterator.hasNext() 15:34:16.104 (104254154)|HEAP_ALLOCATE|[21]|Bytes:8 15:34:16.104 (104263860)|VARIABLE_SCOPE_BEGIN|[21]|opps|Opportunity|true|false 15:34:16.104 (104281235)|VARIABLE_ASSIGNMENT|[21]|opps|{"s":1,"v":{"Id":"0061a00000BMG3hAAH"}}|0x3c2db600 15:34:16.104 (104286705)|STATEMENT_EXECUTE|[22] 15:34:16.104 (104295177)|SYSTEM_METHOD_ENTRY|[23]|List.iterator() 15:34:16.104 (104310524)|SYSTEM_METHOD_EXIT|[23]|List.iterator() 15:34:16.104 (104318485)|SYSTEM_METHOD_ENTRY|[23]|system.ListIterator.hasNext() 15:34:16.104 (104327412)|HEAP_ALLOCATE|[23]|Bytes:5 15:34:16.104 (104333476)|SYSTEM_METHOD_EXIT|[23]|system.ListIterator.hasNext() 15:34:16.104 (104347904)|HEAP_ALLOCATE|[23]|Bytes:12 15:34:16.104 (104357517)|VARIABLE_SCOPE_BEGIN|[23]|engs|Engagement__c|true|false 15:34:16.104 (104376208)|VARIABLE_ASSIGNMENT|[23]|engs|{"s":1,"v":{"Id":"a1x1a000000M9PaAAK","Opportunity_ID__c":"0061a00000BMG3hAAH"}}|0x68a46b99 15:34:16.104 (104381979)|STATEMENT_EXECUTE|[24] 15:34:16.104 (104384329)|STATEMENT_EXECUTE|[25] 15:34:16.104 (104413039)|HEAP_ALLOCATE|[25]|Bytes:18 15:34:16.104 (104448827)|HEAP_ALLOCATE|[25]|Bytes:-4 15:34:16.104 (104462181)|VARIABLE_ASSIGNMENT|[25]|this.Related_Engagement__c|"a1x1a000000M9PaAAK"|0x3c2db600 15:34:16.104 (104466935)|STATEMENT_EXECUTE|[26] 15:34:16.104 (104490388)|HEAP_ALLOCATE|[26]|Bytes:8 15:34:16.104 (104508482)|DML_BEGIN|[26]|Op:Update|Type:Opportunity|Rows:1 15:34:16.104 (104524948)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 15:34:16.106 (106719013)|DML_END|[26] 15:34:16.106 (106825254)|EXCEPTION_THROWN|[26]|System.DmlException: Update failed. First exception on row 0 with id 0061a00000BMG3hAAH; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0061a00000BMG3h) is currently in trigger OpportunityTriggers, therefore it cannot recursively update itself: [] 15:34:16.107 (107278817)|HEAP_ALLOCATE|[26]|Bytes:238 15:34:16.107 (107297056)|METHOD_EXIT|[4]|01p1a000002LI8D|EngagementTriggersClass.executeEng(List, Map) 15:34:16.107 (107371557)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 0061a00000BMG3hAAH; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0061a00000BMG3h) is currently in trigger OpportunityTriggers, therefore it cannot recursively update itself: []

Class.EngagementTriggersClass.executeEng: line 26, column 1 Trigger.EngagementTriggers: line 4, column 1 15:34:16.107 (107388702)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 0061a00000BMG3hAAH; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0061a00000BMG3h) is currently in trigger OpportunityTriggers, therefore it cannot recursively update itself: []

Class.EngagementTriggersClass.executeEng: line 26, column 1 Trigger.EngagementTriggers: line 4, column 1 15:34:16.107 (107403992)|CUMULATIVE_LIMIT_USAGE 15:34:16.107 (107403992)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 3 out of 100 Number of query rows: 2 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 2 out of 150 Number of DML rows: 2 out of 10000 Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10

15:34:16.107 (107403992)|CUMULATIVE_LIMIT_USAGE_END

15:34:16.107 (107472871)|CODE_UNIT_FINISHED|EngagementTriggers on Engagement trigger event AfterInsert for [a1x1a000000M9Pa]

  • Well you have an update inside a for loop, your gonna wanna fix that.. – EricSSH Aug 24 '15 at 19:49
  • What have you tried to fix so far? What are some areas you see as potentially problematic? Just pasting an entire log entry + code and basically repeating the exception (SELF_REFERENCE_FROM_TRIGGER) doesn't show that you've tried to solve it yourself :) – Brian Mansfield Aug 24 '15 at 19:52
  • I was thinking I need to use some kind of boolean flag to stop the recursion, but am not sure where to insert it or where to change it from true to false, false to true. Eric, thanks for the input. I'm trying to get this to work on one record before I bulkify. – Stephen Aug 24 '15 at 20:02
2

To answer your specific question about recursion, yes you are having some issues with recursion. Using a static boolean to control recursion traditionally looks similar to this - using your code as the example of preventing the opportunity trigger logic from executing when manipulating engagement records:

OpportunityTriggersClass - add a static boolean that can be altered from the other trigger's logic

public class OpportunityTriggersClass 
{   
    // by default it is allowed to run
    public static boolean run = true;

    public void ExecuteOpp(List<Opportunity> newList, Map<ID, Opportunity> oldMap)
    {

        // is this an update and am I currently allowed to run?
        if(trigger.isUpdate && OpportunityTriggersClass.run == true)

EngagementTriggersClass - flip the switch to disable the ability for the opportunity trigger logic to run and after DML, flip the switch back.

public class EngagementTriggersClass 
{
     public void executeEng(List<Engagement__c> newList, Map<ID, Engagement__c> oldMap)
   {
        if(trigger.isinsert)
        {               
            List<Engagement__c> newEngagements=[select id, Opportunity_ID__c from Engagement__c where id in:trigger.newmap.keyset()];           
            List<opportunity>forUpdate=new List<opportunity>();

            // SNIP SNIP


            if (forUpdate.size() > 0) {

                // before you fire the update and recurse, disable the OpportunityTrigger's ability to run  
                OpportunityTriggersClass.run = false;

                // perform DML on the Opportunities
                update forUpdate;

                // turn it back on for other operations during this transaction
                OpportunityTriggersClass.run = true;
            }
0

If I understand your requirements and what you are attempting to do here, it looks like the only reason you have the engagement trigger is because you want to set the engagement id. It also looks like you really want to do this in the opportunity trigger but commented it out because the id is not available yet.
However, after you insert the engagement records in your opportunity the ids will then be available. You can loop through the forInsert list and use that to add the opportunity to a separate opportunity list since you will have both ids available, then update the opportunity. Something like:

List<Opportunity> oppsToUpdate = new List<Opportunity>();

    for (Engagement__c eng :forInsert) {
         oppsToUpdate = new Opportunity(Id = eng.Opportunity_ID__c, Related_Engagement__c = eng.Id);
    }

On a side note, looking at your engagement trigger:
Line 7: List<Engagement__c> newEngagements=[select id, Opportunity_ID__c from Engagement__c where id in:trigger.newmap.keyset()]; is not necessary, both these fields should be available in your newList
Line 14: Opportunity newOpp=[select id from opportunity where id=:eng.Opportunity_ID__c]; never put SOQL inside a for loop
Line 23: update opps; you never want to put DML inside a for loop - outer and especially not inner.
But I really don't think you even need the trigger on the engagement. I would recommend breaking this down to first dealing with the after insert. Get that working then try adding in the different contexts. Remember after you insert the engagement records in your trigger the ID will be available.
One other note on the before update section of your trigger, these lines here:

List<engagement__c> EngagementsWithUpdatedParents=[select id, opportunity_id__c from engagement__c where opportunity_id__c in :oldmap.keySet()];
            for(opportunity opp:newList)
            {       
                if(engagementswithupdatedparents.size()>0)   

Make sure you consider what will happen if you update multiple opportunities at the same time, some with related engagement records and some without. Your Engagements list is queried from all updated records but then you loop through each individual opportunity record and say if any of the updated opportunities have an engagement record do something. As far as recursion goes, Marks answer should help with that.

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.