0

In an update trigger on parent object I need to do insert/update/delete on child object depending on criteria in parent object. insert and update works as required. but when delete error thrown as

(SELF_REFERENCE_FROM_TRIGGER, Object (id = a073000000jBt6q) is currently in trigger PopulateFCTPS, therefore it cannot recursively update itself: []: Trigger.PopulateFCTPS: line 54, column 1)

for delete I have separate for loop and different record set. why I get this?

trigger PopulateFCTPS on Assessment_Schedule__c (before update) {
    //map of triggered TPS
    List<Funding_Cycles_TPS__c> fctpsUpdate = new List<Funding_Cycles_TPS__c>();
    //Insert
    for(Assessment_Schedule__c tps:trigger.new){
        if(tps.Assessment_Date__c!=Null && tps.Complete__c==True && tps.FCTPS_Count__c!=1){
            integer yr = tps.Assessment_Date__c.Year();
            Funding_Cycles_TPS__c newfctps = new Funding_Cycles_TPS__c();
            newfctps.Reporting_Year__c =date.newInstance(yr, 01, 01);
            newfctps.Reporting_Year_End__c =date.newInstance(yr, 12, 31);
            newfctps.Training_Plan_Standard__c = tps.id;
            newfctps.Achieved_Credits_Total__c =tps.Credits__c;
            newfctps.Funded_Achieved_Credits__c = tps.Funded_Achieved_Credits__c;
            fctpsUpdate.add(newfctps);} 
    }
    // update fctpsUpdate;
    Map<Id, Assessment_Schedule__c> updatedTPS = new Map<Id, Assessment_Schedule__c>();
    //get old values
    for(Assessment_Schedule__c tps : Trigger.new)
    {
        Assessment_Schedule__c oldtps = Trigger.oldMap.get(tps.Id);
        if(tps.Assessment_Date__c != oldtps.Assessment_Date__c || tps.Complete__c !=oldtps.Complete__c)
        {
            updatedTPS.put(tps.Id, tps);
        }
    }
    if(updatedTPS.size()>0){
        for(Funding_Cycles_TPS__c fctpsAvailable :[Select ID, Training_Plan_Standard__c from
                                                   Funding_Cycles_TPS__c 
                                                   where Training_Plan_Standard__c IN :updatedTPS.keySet()])
        {
            //get parent record from the map for iterated child record  
            Assessment_Schedule__c parentFC = updatedTPS.get(fctpsAvailable.Training_Plan_Standard__c);              
            if(parentFc.Complete__c==True)
            {
                integer yr = parentFC.Assessment_Date__c.Year(); 
                fctpsAvailable.Reporting_Year__c =date.newInstance(yr, 01, 01);
                fctpsAvailable.Reporting_Year_End__c =date.newInstance(yr, 12, 31);
                fctpsUpdate.add(fctpsAvailable);   
                system.debug('==============update==========');
            }
        }    
        upsert fctpsUpdate;    
    }

    //delete
    for (Assessment_Schedule__c del : trigger.new){
        if(del.Complete__c== false){

            delete [select id from Funding_Cycles_TPS__c where Training_Plan_Standard__c IN :trigger.new];
        }
    }
}
  • Is there a MD relationship and rollup fields in play on the funding cycle records that rollup to the assessment schedule? If so that could be causing the issue. – Eric Jun 25 '17 at 8:10
  • Yes, there is one roll-up value – Jay Weera Jun 25 '17 at 8:17
  • Then you will have to go after update with recursion checks or Async as the update of the child causes the parent to update again while it's in this before trigger – Eric Jun 25 '17 at 8:18
0

Ideally, after updating the Parent you should update the child. Here you are updating child first and then updating parent.

So, perform those operations in after update trigger.

| improve this answer | |
  • Hi Santanu, sorry my above trigger heading wrong as i was trying before update. Actually it meant to be trigger PopulateFCTPS on Assessment_Schedule__c (after update) . even with after update the result is same – Jay Weera Jun 24 '17 at 22:30
  • do you have any trigger on Funding_Cycles_TPS__c which is updating Assessment_Schedule__c? – Santanu Boral Jun 24 '17 at 22:39
  • No any single trigger on Funding_Cycles_TPS__c – Jay Weera Jun 24 '17 at 22:59

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.