0

Below is the Trigger and Test Class. Trigger works perfect and assign Entitlement to Case as soon as Case is created via Email to Case. The Weird Part is Dev Console shows 62% Code Coverage, while when I us Mavensmate it shows 45%. Nonetheless and I need some pointers to write a better Test class so that I can get better Coverage for Trigger.

Below is the Trigger and Test Class with Lines that are not covered are highlighted.

Thank you in advance!

Trigger:

trigger DefaultEntitlement on Case (Before Insert, Before Update) {
   /*
   If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement
    and select the first one.  If not then check to see if the Account on the Case has an active Entitlement.
   */
   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);
      }
   }
   if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
      /* Added check for active entitlement */
      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;*****
                  }
               } // end for
            }
         } // end for
      } 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;***
                     }
                  } // end for
               }
            } // end for
         }
      }
   } // end if(contactIds.isEmpty()==false)
}

Test Class:

@isTest 

    public class defaultEntitlementTest 
    {
        static testMethod void testMethod1() 
        {
            Account acc = new Account();
            acc.Name ='Test';
            insert acc;

            Contact cont = new Contact();
            cont.LastName ='Test';
            cont.accountid = acc.id;
            insert cont;

            Case caseObj = new Case(
                ContactId = cont.id,
                AccountId = acc.id,
                Status = 'Working',
                Origin = 'Phone');

            insert caseObj;



        }
    }
1
  • is my answer helps? Commented Jun 9, 2017 at 14:12

1 Answer 1

0

You have to query Entitlement Process whose API is SlaProcess as Salesforce doesn't allow to create that from test class.

Replace <Your Entitlement Process at your org> which is defined as your org.

Then create Entitlement and EntitlementContact records based on Account and Contact.

After that create Case record.

List<SlaProcess> lstSlaProcess = [SELECT Id, description, name From SlaProcess Where Name='<Your Entitlement Process at your org>'];
if(lstSlaProcess.size()>0)
{
    //create Entitlement record
    Entitlement entl = new Entitlement();
    entl.SlaProcessId = lstSlaProcess[0].Id;
    entl.accountId = acc.Id;
    //define other attributes
    insert entl;

    //create EntitlementContact record
    EntitlementContact entlCon = new EntitlementContact();
    entlCon.EntitlementId = entl.Id;
    entlCon.ContactId = cont.Id;
    //define other attributes
    insert entlCon;
}
//now create case

Follow Testing Best Practices for writing test class and testing.

1
  • Thanks Santanu. Let me try this and update back here. Thanks again for your time!
    – sfdcDev
    Commented Jun 10, 2017 at 17:03

You must log in to answer this question.

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