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
John McAfeeJohn McAfee 

test case for entitlement Trigger

Hey all, I am new to to Apex code and wanted to see if anyone could help me with a test class for this trigger..  also thanks to whoever wrote this.. the community is awesome.
I just need to match entitlement to a case based on the account in lighning. thanks in advance. 

trigger DefaultEntitlement on Case (Before Insert, Before Update) {
List<Id> contactIds = new List<Id>(); 
    List<Id> acctIds = new List<Id>();
    for (Case c :{
    if (c.EntitlementId == null && c.ContactId != null && c.AccountId != null){
    if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
    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];
            for(Case c :{
                if(c.EntitlementId == null && c.ContactId != null){
                    for(EntitlementContact ec:entlContacts){
                            c.EntitlementId = ec.EntitlementId;
                            if(c.AssetId==null && ec.Entitlement.AssetId!=null)
        } 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];
                for(Case c :{
                    if(c.EntitlementId == null && c.AccountId != null){
                        for(Entitlement e:entls){
                                c.EntitlementId = e.Id;
                                if(c.AssetId==null && e.AssetId!=null)
Hi John ,

Writting test class in salesforce is very easy . You need to understand the scenario and proper relationship between  the objects and insert/update  records as per the scenario .

Please check below links so that you can start writting test class . (

Try to run the test class in you rorganisation , you can install code coverage calculator chrome extension which will help you to identify the line which covered .

Still you are facing problem ,please let us know we will help for sure .

Thanks ,
John McAfeeJohn McAfee
Thanks for the help. I was able to get it tested but now have another issue I cant figure out if its just a timing thing or what..  when I create a case and set the contact AND the account then the trigger works and sets the entitlement..   if I just add the contact, the basic matching in salesforce works and adds the correct account to the case but then the trigger does not update the entitlement. Does the trigger fire before the contact and account matching function or after or does there need to be a delay of some sort ??  

Thanks for any insight
John McAfeeJohn McAfee
More thoughts

I am not very good with Apex code but looking at the trigger , I am not seeing where there is a lookup to get and set the account ID.  I am thinking that acctIds is empty resulting in the trigger not grabbing the entitlement. 

Is there a logging line that can be put in to check to see if acount ID is being set AND / OR is there a way for this to run after the matching that salesforce does on the email address to set the account..  

Thanks in advance.