+ Start a Discussion
Rahul H 4Rahul H 4 

apex class issues surfaced. Need help

I have written the Apex trigger for Case entitlement.
 
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
List<Id> contactIds = new List<Id>();
List<Id> acctIds = new List<Id>();
for (Case c : Trigger.new){
if ( c.EntitlementId == null && c.ContactId != null && c.AccountId != null){
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
}
system.Debug('acctIds'+acctIds);
system.Debug('contactIds'+contactIds);
List <EntitlementContact> entlContacts =
[Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId
From EntitlementContact e
Where e.ContactId in :contactIds
And e.Entitlement.EndDate >= Today
And e.Entitlement.StartDate <= Today];
if(entlContacts.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.ContactId != null){
for(EntitlementContact ec:entlContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId=ec.Entitlement.AssetId;
break;
}
}
}
}
} else{
List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate,
e.AccountId, e.AssetId
From Entitlement e
Where e.AccountId in :acctIds And e.EndDate >= Today
And e.StartDate <= Today];
if(entls.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.AccountId != null){
for(Entitlement e:entls){
if(e.AccountId==c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
}
}
}
}
}
}
 
The Apex trigger is fine and no syntax issues are surfaced. But, the code coverage is only 444% so I am trying to write the Apex 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);
 
Id contactId = cont.Id;
 
Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id);
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);
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());
}
}
}
 
But, it seems to have some problem.
 
Can you please check and advise.
 
Thanks
 
Best Answer chosen by Rahul H 4
Steven NsubugaSteven Nsubuga
Try this
@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);
        
        Id contactId = cont.Id;
        
        Asset a = new Asset();
        a.AccountId = busAcc.id;
        a.Name = 'Test';
        a.Status = 'Active';
        insert a;
        
        
        Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id, AssetId = a.Id,
                                           StartDate = date.today().addDays(-3), EndDate = date.today().addDays(3));
        insert entl;
        
        EntitlementContact entContact = new EntitlementContact( EntitlementId = entl.Id, 
                                           ContactId = contactId);
        insert entContact;
        
        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);
            }
        cases.add(new Case(Subject = 'Test Case without Entitlement', ContactId = contactId, accountid = busAcc.id));
        
        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());
        }
    }
    
    //no entitlementContact
    static testMethod void TestCompleteMilestoneCase2(){
        
        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);
        
        Id contactId = cont.Id;
        
        Asset a = new Asset();
        a.AccountId = busAcc.id;
        a.Name = 'Test';
        a.Status = 'Active';
        insert a;
        
        Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id, AssetId = a.Id,
                                           StartDate = date.today().addDays(-3), EndDate = date.today().addDays(3));
        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);
                cases.add(c);
            }
        cases.add(new Case(Subject = 'Test Case without Entitlement', ContactId = contactId, accountid = busAcc.id));
        
        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());
        }
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
Try this
@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);
        
        Id contactId = cont.Id;
        
        Asset a = new Asset();
        a.AccountId = busAcc.id;
        a.Name = 'Test';
        a.Status = 'Active';
        insert a;
        
        
        Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id, AssetId = a.Id,
                                           StartDate = date.today().addDays(-3), EndDate = date.today().addDays(3));
        insert entl;
        
        EntitlementContact entContact = new EntitlementContact( EntitlementId = entl.Id, 
                                           ContactId = contactId);
        insert entContact;
        
        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);
            }
        cases.add(new Case(Subject = 'Test Case without Entitlement', ContactId = contactId, accountid = busAcc.id));
        
        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());
        }
    }
    
    //no entitlementContact
    static testMethod void TestCompleteMilestoneCase2(){
        
        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);
        
        Id contactId = cont.Id;
        
        Asset a = new Asset();
        a.AccountId = busAcc.id;
        a.Name = 'Test';
        a.Status = 'Active';
        insert a;
        
        Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id, AssetId = a.Id,
                                           StartDate = date.today().addDays(-3), EndDate = date.today().addDays(3));
        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);
                cases.add(c);
            }
        cases.add(new Case(Subject = 'Test Case without Entitlement', ContactId = contactId, accountid = busAcc.id));
        
        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());
        }
    }
}

 
This was selected as the best answer
Rahul H 4Rahul H 4
Thanks