0

Recently I stumbled upon problem getting code coverage for Apex code that evaluates case milestones by their target date values. When trying to create case in test class it does not create case milestone for it after insert although all the criteria is being met what is defined in entitlement process.

Also by reading articles - like this about entitlements it seems like I have created everything. The Entitlement process is Active, Entitlement with associated Account that is linked to Case is created and linked to Case, Business Hours are valid, Entitlement process is linked to Entitlement. Case entry criteria matches what is defined in Entitlement Process for Milestones, but still in test class milestones are not created.

System.debug('case milestones = '+ cmss); statement returns empty list and logic that relies on case milestone target date cannot be covered. Is there something missing in test data?

Example of test class:

@IsTest private class GetCaseTest {

@TestSetup
static void createTestData(){

    Profile p = [SELECT Id FROM Profile WHERE Name='Customer Service Lightning'];
    UserRole role = [SELECT ID, Name FROM UserRole WHERE Name Like 'NO%' LIMIT 1];
    System.debug('@role: ' + role);

    User u = new User(
            Alias = 'standt',
            Email='testmail@test.com',
            EmailEncodingKey='UTF-8',
            FirstName='Test',
            LastName='Testing',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            ProfileId = p.Id,
            UserRoleId=role.Id,
            TimeZoneSidKey='America/Los_Angeles',
            UserName='testmail@test.com'
    );
    insert u;
    Group g = new Group(
            Name='NO CS',
            Type = 'Queue'

    );
    insert g;

    queuesobject q1 = new queuesobject(SObjectType = 'Case', queueid=g.id);
    insert q1;
    GroupMember member = new GroupMember();
    member.UserOrGroupId = u.Id;
    member.GroupId = g.Id;

    insert member;
    
}


@isTest
static void getCaseFuture(){
    User u = [SELECT Id, Email FROM User WHERE Email = 'testmail@test.com'].get(0);

    System.runAs(u){

    Id accRecType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('B2B Account').getRecordTypeId();
    Account acc = new account(name='Test Account', RecordTypeId= accRecType, OwnerId = u.Id);
    insert acc;

    Contact oContact = new contact(firstname='John',lastname='Doe',email='test@test.com',accountid=acc.id, OwnerId = u.Id);
    insert oContact;

    Entitlement entl = new entitlement(name='NO Entitlement',accountid=acc.id, StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));
    insert entl;

    List<SlaProcess> lstEntitlementProcess = [SELECT Id, Name FROM SlaProcess WHERE Name='NO Entitlement Process 1.3' and IsActive = true LIMIT 1];
    System.debug('lstEntitlementProcess= '+lstEntitlementProcess);
    entl.SlaProcessId = lstEntitlementProcess[0].id;
    update entl;
        BusinessHours bhours = [SELECT Id, Name FROM BusinessHours WHERE Name = 'NO Customer Service'].get(0);
        Id recordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Card Administration').getRecordTypeId();
        Group g = [SELECT Id FROM Group WHERE Type = 'Queue' AND Name = 'NO CS'].get(0);
        Case c2 = new Case(
                Subject = 'To be distributed from queue',
                Status = 'In Progress',
                Case_Re_opened__c = false,
                OwnerId = g.Id,
                CurrencyIsoCode = 'EUR',
                Country__c = 'NO',
                Case_Category__c = 'Card Block',
                RecordTypeId = recordTypeId,
                AccountId = acc.Id,
                ContactId = oContact.Id,
                EntitlementId = entl.Id,
                slaStartDate=system.now(),
               BusinessHoursId = bhours.Id
        );
        insert c2;

        List<CaseMilestone> cmss = [SELECT Id, CaseId FROM CaseMilestone WHERE CaseId =: c2.Id];

        System.debug('case milestones = '+ cmss);
        System.debug('c2 = '+ c2);
        System.debug('c2.OwnerId=> '+ c2.OwnerId);
    }
    Test.startTest();
    GetCase.findCase();
   // GetCase.findCase();
    Test.stopTest();
}

}

Pmal12
  • 53
  • 1
  • 6

0 Answers0