function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Doug RamirezDoug Ramirez 

How to automatically un-complete case milestones

I have implemented a set of Apex triggers to automatically mark case milestones as complete when a case has an initial 'Send an Email' or 'Log a Call' has been done.  This all works fine.

 

However, I have an additional use case to un-complete case milestones when a case is re-opened.  Right now the last case milestone is marked as complete when a case is closed.  So in the event that a case is re-opened (Case.Status != 'Closed') the last case milestone should be marked as incomplete.  However when this happens in the context of a before update or after update trigger an exception is thrown because the entitlement process for the case hasn't completely exited.

 

When a case is re-opened the 'Entitlement Process End Time' is set to null and the case milestones can be edited manually.  So, I suspect that the scope of the transaction to set the case milestone to incomplete is being submitted with the setting of the end time to null.  Which means the case has still already exited the entitlement process when the trigger is trying update the milestone.

 

Here is snippet of the code from the on Case (after update):

 

trigger OpenMilestoneOnReopenCase on Case (after update) 
{
// The final resolution case milestone type's name is Final Resolution Time
	String finalResolutionCaseMilestoneName = 'Final Resolution Time';
	if (inDebugMode) System.Debug('OpenMilestoneOnReopenCase: finalResolutionCaseMilestoneName is ' + finalResolutionCaseMilestoneName);
    
   for (Case cas: Trigger.new)
    {
    	// If the case is being closed then exit
    	if (cas.Status == 'Closed')
    	{
    		continue;
    	}
    	
    	// Get each case milestone for the case
    	CaseMilestone[] caseMilestones = CaseHelpers.GetCaseMilestones(cas.Id);
    	for (CaseMilestone caseMilestone : caseMilestones)
    	{	
    		// If the case milestone is already incomplete then skip it because we only want to reopen completed milestones
    		if (caseMilestone.IsCompleted == false)
    		{
    			continue;
    		}
    		
    		// Get the milestone type so we can check to see if it's the final resolution case milestone
    		MilestoneType milestoneType = CaseHelpers.GetMilestoneType(caseMilestone.MilestoneTypeId);
			
		// Check to see if this case milestone type is the first final resolution milestone
		if (milestoneType.Name == finalResolutionCaseMilestoneName)
			{
				// Check to see if the final resolution case milestone is complete
				if (caseMilestone.IsCompleted == true)
				{
					// If it's complete then mark it as incomplete by nulling out the completion date
					// after nulling out the entitlement process end time
					caseMilestone.CompletionDate = NULL;
					database.update(caseMilestone);
				}

			}
    	}	
    }
}

 

I am interested in knowing if anyone can confirm my suspicions about the scope of the update transaction.  And if there any suggestions to managing the exit of the process and the setting of a case milestone as incomplete.

 

Doug

 

 

paulo.orquillopaulo.orquillo

Hi Doug,

 

I know the post is quite old and taking a shot at this. Did you ever resolve this issue? As I have quite a similar requirement where completed milestones will have the completion date cleared when a case is reopened. 

 

Thanks,

Paulo

Houdini-GHoudini-G
Use @future on the update method.