Working off of the code provided here https://help.salesforce.com/s/articleView?id=sf.entitlements_milestones_trigger.htm&type=5

The test class does not cover required coverage specially the if condition in the below code. Any inputs and help is appreciated.

Apex Class

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 
                                           and completionDate = null limit 1];

        if (cmsToUpdate.isEmpty() == false){
            for (CaseMilestone cm : cmsToUpdate){
                cm.completionDate = complDate;
            update cmsToUpdate;

Here is the test class method

 @isTest static void updateMileStnComplDateTest(){
        List<Account> acts = new List<Account>();
        Account myAcc = new Account(Name='TestAct', phone='1001231234');
        Account busAcc = new Account(Name = 'TestForMS', phone='4567890999');
        insert acts;

        Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', 
                                   phone='4567890999', accountid = busAcc.id);

        SlaProcess sla = [SELECT Id FROM SlaProcess WHERE isActive = true LIMIT 1];   
        Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id, 
                                          slaProcessId = sla.Id);
        insert entl;
        List<Case> cases = new List<Case>{};
        Case c = new Case(Subject = 'Test Case with Entitlement ', 
                          EntitlementId = entl.Id, ContactId = cont.Id,
                          Status = 'Closed', SlaStartDate = date.today() - 5 
        insert cases;

        List<Id> caseIds = new List<Id>();
        for (Case cL : cases){
        milestoneUtils.completeMilestone(caseIds, 'Resolution Time', System.now());

1 Answer 1


The if statement is testing if Case Milestones were found for the case Id(s) and Milestone Types passed to the completeMilestone method. If it is not being covered, that means no such milestones were found. The sample unit test code provided in the help document assumes you have an entitlement process that will create a Milestone of type 'Resolution Time' on the Case created during the test. You may need to modify this to match your actual entitlement process.

Not the answer you're looking for? Browse other questions tagged .