triggerssalesforcecasetest-class

Test coverage for test class of trigger is showing 53 constantly


I am new to salesforce development.I have written test class for following trigger, which is having coverage as 53 only. I tried several times by adding and changing code lines into test class but it is still showing 53. Could you please help me to resolve this?

Thanks in advance. any help would be appreciated.

        trigger CaseTrigger on Case (before insert,before update, after update) 
{
    if((Trigger.isBefore && Trigger.isUpdate)||(Trigger.isBefore && Trigger.isInsert))
    {

   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 entitlement
            }
         } // end for case
      } 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)

    }


    if(Trigger.isBefore && Trigger.isUpdate)
    {
        System.debug('entered if condition');

        for(Case c : Trigger.new)
        {
            if (c.Assignment_Group__c != trigger.oldMap.get(c.Id).Assignment_Group__c){
            System.debug('Updating logic');
            List<Group> qid = [select Id from Group where Name = : c.Assignment_Group__c and Type = 'Queue'];
              for(Group g : qid)
                {
                    c.OwnerId = g.id;
                    System.debug('updated');
                }
            }}        
    }

     if(Trigger.isBefore && Trigger.isUpdate)
    {
      for(Case c : Trigger.new)
        {   
            if(c.Assignment_Group__c=='Tech Support'||c.Assignment_Group__c=='GD-IT'||c.Assignment_Group__c=='App-Support'||c.Assignment_Group__c=='GD-RM'||c.Assignment_Group__c=='GD-DB'||c.Assignment_Group__c=='Dev-Ops'||c.Assignment_Group__c=='App-Management'||c.Assignment_Group__c=='PDT-DS-Engg'||c.Assignment_Group__c=='PDT-US-Engg')
            {
                c.Group_Manager_Email__c = 'sargware@gmail.com'; c.Escalation_Level_2_Email__c ='sargware@gmail.com'; c.Escalation_Level_3_Email__c='sargware@gmail.com';
            }
        }
    }

    if(Trigger.isBefore && Trigger.isUpdate)
    {
      for(Case c : Trigger.new)
        {   
            if(c.Internal_Impact__c == 'Low' && c.Internal_Urgency__c=='Low')
            {
                c.Priority='Planning';
            }
            if(c.Internal_Impact__c == 'High' && c.Internal_Urgency__c=='High')
            {
                c.Priority='Critical';
            }
            if(c.Status=='Pending User' || c.Status=='Pending Vendor')
            {
                c.IsStopped = True;
            }

        }

      }

    if((Trigger.isAfter && Trigger.isUpdate))
    {
    Set<Id> Ids = new Set<Id>();
    Set<Id> IdR = new Set<Id>();
    Set<Id> IdC = new Set<Id>();
    List<SC_Problem_Case_Link__c> scprblm = new List<SC_Problem_Case_Link__c>();
    List<SC_Problem_Case_Link__c> scprblmo = new List<SC_Problem_Case_Link__c>();
    List<SC_Problem_Case_Link__c> scprblmc = new List<SC_Problem_Case_Link__c>();
     for (Case cs: Trigger.new)
    {
        Case oldLead = Trigger.oldMap.get(cs.Id);

        if (cs.Status == 'Tested' && Trigger.oldMap.get(oldLead .Id).Status=='Ready for Testing')
        { 
            Ids.add(cs.Id);
        }
        if (cs.Status == 'ReOpen')
        { 
            IdR.add(cs.Id);
        }
        if (cs.Status == 'Closed')
        { 
            IdC.add(cs.Id);
        }
    }

    for (SC_Problem_Case_Link__c rateSheet: [select Id,Status__c from SC_Problem_Case_Link__c where Status__c = 'Ready for Testing' AND Case__c in :Ids])
    {

        if (rateSheet.Status__c != 'Tested') {
            rateSheet.Status__c = 'Tested';
            //rateSheet.Case__c.Status= 'Awaiting Deployment';
            scprblm.add(rateSheet);
        }

    }
    if (!scprblm.isEmpty()) {
        update scprblm;
    }


   for (SC_Problem_Case_Link__c rateSheet: [select Id,Status__c from SC_Problem_Case_Link__c where Status__c = 'Resolved' AND Case__c in :IdR])
    {
    // List<SC_Problem_Case_Link__c> accts = [Select Id, Status__c from SC_Problem_Case_Link__c where Id in : Trigger.old];

        if (rateSheet.Status__c != 'Open') {
            rateSheet.Status__c = 'Open';
            scprblmo.add(rateSheet);
        }

    }
    if (!scprblmo.isEmpty()) {
        update scprblmo;
    }


   for (SC_Problem_Case_Link__c rateSheet: [select Id,Status__c from SC_Problem_Case_Link__c where Status__c = 'Resolved' AND Case__c in :IdC])
    {

        if (rateSheet.Status__c != 'Closed') {
            rateSheet.Status__c = 'Closed';
            scprblmc.add(rateSheet);
        }

    }
    if (!scprblmc.isEmpty()) {
        update scprblmc;
    }
}
}

Solution

  • Hi SAdmin and welcome to the community.

    You can use Developer Console in order to check which part of your code is not covered on your test class. Every time you run your test class you are able to see which lines of code is covered by clicking on Code Coverage dropdown on your main apex class.

    This way you can see that the lines of code that are covered by tests are blue. Lines of code that aren’t covered are red. Lines of code that don’t require coverage (for example, curly brackets, comments, and System.debug calls) are left white.

    Please check this guide out to find some more info.