You need to sign in to do that
Don't have an account?

Auto Complete Response Time Milestone
Hi all,
I use apex trigger to auto complete a milestone "Response Time", it works but I have to create a test apex class to import it in production and the code coverage is only 70%.
Can you help me?
My test class :
My apex class MilestoneUtils:
Problem is cmsToUpdate is empty because Case Milestione isn't created.
I don't understand why
I use apex trigger to auto complete a milestone "Response Time", it works but I have to create a test apex class to import it in production and the code coverage is only 70%.
Can you help me?
My test class :
@isTest private class MilestoneTest { static testMethod void TestCompleteMilestoneCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Contact oContact = [select id from Contact limit 1]; String contactId; if (oContact != null) contactId = oContact.Id; Asset ast = new Asset(Name='support platinum', AccountId=busAcc.Id, Start_date__c = date.today().addDays(-5), End_date__c = date.today().addDays(5)); insert ast; ast = [SELECT Id, Name, Active__c, Start_date__c, End_date__c, AccountId FROM Asset WHERE Id=:ast.Id]; String astId; if (astId != null) astId = ast.Id; Entitlement entl = new Entitlement(Name='First Response - SUPPORT PLATINUM PLAN - TestForMS', AccountId=busAcc.Id, AssetId = astId, EndDate= date.today().addDays(5), StartDate= date.today()); insert entl; 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, Description = 'test', Departement__c = 'Centreon', AssetId = astId, Priority = 'Blocking'); cases.add(c); } 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()); } } }
My apex class MilestoneUtils:
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; } else{ List<CaseMilestone> cmsTest = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds limit 1]; update cmsTest; } } }
Problem is cmsToUpdate is empty because Case Milestione isn't created.
I don't understand why
I'm not sure that we can create casemilestone manually