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
frankal4frankal4 

Is there a way to update the completion date on a milestone when a case is created? (New Case page)

Hello all,

 

Is there a way, is it possible, to have a completion date on a milestone populate when a New Case is saved? Either through workflow or code?

I have tried through workflow and code but have been unsuccessful so far. (new to SaleForce:smileysurprised: ).

I have a tried both but have been unsuccessful so far.

 

Thank you for any help you can give!

Frank

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Apex Code is (most likely) the way to go. However, I don't know what your object relationships look like, so I can't provide a concrete example, but the general syntax for a trigger like this might be:

 

 

trigger updateMilestone on Case(after insert) {
  Map<Id,Milestone__c> milestones = new Map<Id,Milestone__c>();
  for(Case c:Trigger.new)
    if(c.Milestone__c<>null)
      milestones.put(c.Milestone__c,new Milestone__c(id=c.Milestone__c,CompletedDate__c=System.today());
  update milestones.values();
}

Triggers are almost universally a matter of creating a list or map, iterating through some list of values to populate the list or map, and then performing some action on the list or map as a whole.

 

All Answers

sfdcfoxsfdcfox

Apex Code is (most likely) the way to go. However, I don't know what your object relationships look like, so I can't provide a concrete example, but the general syntax for a trigger like this might be:

 

 

trigger updateMilestone on Case(after insert) {
  Map<Id,Milestone__c> milestones = new Map<Id,Milestone__c>();
  for(Case c:Trigger.new)
    if(c.Milestone__c<>null)
      milestones.put(c.Milestone__c,new Milestone__c(id=c.Milestone__c,CompletedDate__c=System.today());
  update milestones.values();
}

Triggers are almost universally a matter of creating a list or map, iterating through some list of values to populate the list or map, and then performing some action on the list or map as a whole.

 

This was selected as the best answer
frankal4frankal4

Thank you for detailed reply! It answers the question perfectly!

PAAPAA
The things can be achieved using below code.
 
public class MilestoneUtils {
	public static void completeMilestone(List<Id> caseIds,String milestoneName, DateTime complDate) {
		List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName
				and completionDate = null limit 1];
				
		if (cmsToUpdate.isEmpty() == false){
			for (CaseMilestone cm : cmsToUpdate){
				cm.completionDate = complDate;
			}
			update cmsToUpdate;
		}
	}
}