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
Adelchi PelizzoAdelchi Pelizzo 

Why do I get error Error: Method does not exist or incorrect signature: milestoneUtils.completeMilestone(LIST<Id>, String, Datetime)

I am trying to create this trigger:

trigger completeFirstResponseCaseComment on CaseComment (after insert) {

// Cannot be a portal user
if (UserInfo.getUserType() == 'Standard'){
  DateTime completionDate = System.now();
  List<Id> caseIds = new List<Id>();
  for (CaseComment cc : Trigger.new){
   // Only public comments qualify
   if(cc.IsPublished == true)
    caseIds.add(cc.ParentId);
  }
  if (caseIds.isEmpty() == false){
   List<Case> caseList = [Select c.Id, c.ContactId, c.Contact.Email,
            c.OwnerId, c.Status,
            c.EntitlementId, c.SlaStartDate,
            c.SlaExitDate
            From Case c
            Where c.Id IN :caseIds];
   if (caseList.isEmpty() == false){
    List<Id> updateCases = new List<Id>();
    for (Case caseObj:caseList) {
     // consider an outbound email to the contact on the case a valid first response
     if ((caseObj.Status == 'In Progress')&&
         (caseObj.EntitlementId != null)&&
         (caseObj.SlaStartDate <= completionDate)&&
         (caseObj.SlaStartDate != null)&&
         (caseObj.SlaExitDate == null))
      updateCases.add(caseObj.Id);
    }
    if(updateCases.isEmpty() == false)
     milestoneUtils.completeMilestone(updateCases, 'First Response', completionDate);
   }
  }
}
}

Then I try to create a milestoneutils 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 cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1];
    if (cmsToUpdate.isEmpty() == false){
      for (CaseMilestone cm : cmsToUpdate){
        cm.completionDate = complDate;
      }
      update cmsToUpdate;
    } // end if
  }
 
  // test methods
  static testMethod void testCompleteMilestoneCase(){
   
    Contact oContact = [select id from Contact limit 1];
    String contactId;
    if (oContact != null)
      contactId = oContact.Id;
   
    Entitlement entl = [select id from Entitlement limit 1];
    String entlId;
    if (entl != null)
      entlId = entl.Id;
   
    List<Case> cases = new List<Case>{};
    if (entlId != null){
      Case c = new Case(Subject = 'Test Case with Entitlement ', EntitlementId = entlId, ContactId = contactId);
      cases.add(c);
    }
   
    // Insert the Account records that cause the trigger to execute.
    if (cases.isEmpty()==false){
      insert cases;
      List<Id> caseIds = new List<Id>();
      for (Case cL : cases){
        caseIds.add(cL.Id);
      }
      milestoneUtils.completeMilestone(caseIds, 'First Response', System.now());
        }
    }
 
    static testMethod void testCompleteMilestoneViaCase(){
     
        // Perform data preparation
        Entitlement entl = [select id from Entitlement limit 1];
        String entlId;
        if (entl != null)
            entlId = entl.Id;
        List<Case> cases = new List<Case>{};
        for(Integer i = 0; i < 1; i++){
            Case c = new Case(Subject = 'Test Case ' + i);
            cases.add(c);
            if (entlId != null){
                c = new Case(Subject = 'Test Case with Entitlement ' + i, EntitlementId = entlId);
                cases.add(c);
            }
        }
       
        // Insert the Account records that cause the trigger to execute.
        insert cases;

        List<CaseComment> ccs = new List<CaseComment>{};
        for(Case c : cases){
            CaseComment cc = new CaseComment(CommentBody='TestPublic', IsPublished=true, ParentId=c.Id);
            ccs.add(cc);
            cc = new CaseComment(CommentBody='TestPrivate', IsPublished=false, ParentId=c.Id);
            ccs.add(cc);
        }
        if (ccs.isEmpty()==false)
            insert ccs;
   
    // Now create emailmessage objects for them.
   
        List<EmailMessage> emails = new List<EmailMessage>();
        for(Case c : cases){
            emails.add(new EmailMessage(parentId = c.id));
        }
        if(emails.isEmpty()==false)
            database.insert(emails);
       
        for(Case c : cases){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddr = new String[] {'mramsey@salesforce.com'};
            mail.setToAddresses(toAddr);
            mail.setSaveAsActivity(false);
            mail.setTargetObjectId(c.ContactId);
            mail.setWhatId(c.Id);
            mail.setHtmlBody('TestHTMLBody');
            mail.setPlainTextBody('TestTextBody');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
   
    for(Case c : cases){
      c.Status = 'Closed';
    }
    update cases;
   
        // Query the database for the newly inserted records.
        List<Case> insertedCases = [SELECT Subject,
                                           Description,
                                          (SELECT IsPublished, CommentBody From CaseComments),
                                          (SELECT TextBody, Subject, Incoming From EmailMessages)
                                           FROM Case
                                           WHERE Id IN :cases];
    }
}

But still not luck, I get this error:
: Compile Error: Didn't understand relationship 'EmailMessages' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 103 column 36
Anil SavaliyaAnil Savaliya
Hey Adelchi,

Can you try below piece of code changing in your code.

       // Query the database for the newly inserted records.
        List<Case> insertedCases = [SELECT Subject,
                                           Description,CaseComments__r.IsPublished,CaseComments__r.CommentBody
                                           EmailMessages__r.TextBody,EmailMessages__r.Subject,EmailMessages__r.Incoming
                                           FROM Case
                                           WHERE Id IN :cases];

Ramu_SFDCRamu_SFDC
Try changing the completionDate to datetime before passing it to the method. Follow the solution provided in the below post for changing the date to datetime

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008kuMIAQ
Adelchi PelizzoAdelchi Pelizzo
Anil> Error: Compile Error: Didn't understand relationship 'CaseComments__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 103 column 37
Adelchi PelizzoAdelchi Pelizzo
Ramu > Can you tell me exactly where should I change completionDate because I am still getting error "unespected token"