+ Start a Discussion
case commentscase comments 

trigger exceeding SOQL lmits

I have a project application that has milestones and tasks.  Milestones can have multiple tasks.  On Milestones, there can be a lookup to another milestone, which also has it's own tasks.

My trigger updates all the dates of the tasks and the related milestone when the original milestone's date changes.  The problem I'm running into is that when the trigger updates the related milestone, that then runs the trigger again, since it's and update to milestone.

The code works if i'm updating a milestone with only 2 levels of related milestones, but once I get to something higher, I run into soql limits.  Is there a more efficient way for me to write this trigger?

 

trigger MilestoneTrigger on MPM4_BASE__Milestone1_Milestone__c (after update) {
    Integer DaysToAdd;
    
    //get ID of Milestones
    Set<ID> msid = new Set<ID>();
    for(MPM4_BASE__Milestone1_Milestone__c ms: trigger.new){
        msid.add(ms.id);
        //if kickoff date is changed then do code
        //calculate the day difference of old and new date    
        MPM4_BASE__Milestone1_Milestone__c oldms = Trigger.oldMap.get(ms.Id);
        DaysToAdd = oldms.MPM4_BASE__Kickoff__c.daysBetween(ms.MPM4_BASE__Kickoff__c);
    }
    
    //get list of all tasks in milestone
    List<MPM4_BASE__Milestone1_Task__c> tasks = [SELECT Id, MPM4_BASE__Due_Date__c,MPM4_BASE__Start_Date__c FROM MPM4_BASE__Milestone1_Task__c WHERE MPM4_BASE__Project_Milestone__c IN: msid];
    //get list of all child milestones
    List<MPM4_BASE__Milestone1_Milestone__c> cmilestone = [SELECT Id, MPM4_BASE__Deadline__c, MPM4_BASE__Kickoff__c FROM MPM4_BASE__Milestone1_Milestone__c WHERE MPM4_BASE__Predecessor_Milestone__c IN: msid ];  
    
    
    
    List<MPM4_BASE__Milestone1_Task__c> updateTask = new List<MPM4_BASE__Milestone1_Task__c>();
    List<MPM4_BASE__Milestone1_Milestone__c> updateCMilestone = new List<MPM4_BASE__Milestone1_Milestone__c>();
    
    
    //add those dates to all tasks
    for(MPM4_BASE__Milestone1_Task__c t : tasks){
        t.MPM4_BASE__Due_Date__c += DaysToAdd;
        t.MPM4_BASE__Start_Date__c += DaysToAdd;
        updateTask.add(t);
    }
    if(updateTask.isEmpty()==false){
        update updateTask;}
    
    // add those dates to child milestones dates
    for(MPM4_BASE__Milestone1_Milestone__c m : cmilestone){
        m.MPM4_BASE__Deadline__c += DaysToAdd;
        m.MPM4_BASE__Kickoff__c += DaysToAdd;
        updateCMileStone.add(m);
    }  
    if(updateCMilestone.isEmpty()==false){
        update updateCMilestone;}
    
}

 

Samba GSamba G
 for(MPM4_BASE__Milestone1_Milestone__c ms: trigger.new){
        
        //if kickoff date is changed then do code
        //calculate the day difference of old and new date    
        MPM4_BASE__Milestone1_Milestone__c oldms = Trigger.oldMap.get(ms.Id);
        
        DaysToAdd = oldms.MPM4_BASE__Kickoff__c.daysBetween(ms.MPM4_BASE__Kickoff__c);
        if(oldms.MPM4_BASE__Kickoff__c != ms.MPM4_BASE__Kickoff__c) {
            msid.add(ms.id);
        }
}

 Check it out.

 

Thanks,

Samba

 

Email:samba.gao@hotmail.com

Skype: meginfo.samba.gao