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];
}
}
}