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
ChinnoduChinnodu 

I am writing a test class to the below trigger,i got only 50% code coverage but iam looking more than 75%.

Hi Experts,
I am writing a test class to the below  trigger, in our test class  am using @isTest(SeeAllData=true) then I got code cover age , according to our requirement am using only @istest then I didn’t received the code coverage , then am getting bellow error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, emailtoQueueonCaseOwnerchange: execution of AfterUpdate

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.emailtoQueueonCaseOwnerchange: line 19, column 1: []
trigger ParentCaseGBOAge on Case ( after update) 
{

    Set<Id> recordIds = new Set<Id>();
    Set<Id> parentIds = new Set <Id>();
       
    for (case c : Trigger.new)
        
        if( AvoidRecursion.isFirstRun())
    {
        if ( c.RecordTypeId == Schema.Sobjecttype.case.getRecordTypeInfosByName().get('GBOCase').getRecordTypeId() && c.parentid == null && c.status == 'Closed')
             {
             parentIds.add(c.id);
             }
    }
    system.debug('@@@@'+ParentIds.size());
    if (parentIds.isEmpty() == false) 
      {
     
        //List <case> caseparent = [Select id, Parent_BDT_Case_Age__c,Parent_COM_Case_Age__c,Parent_LSR_Case_Age__c, Parent_PDT_Case_Age__c   from case where id in :ParentIds ];
        Map<ID,Case> ResultMap = new Map <ID,Case> ([Select id, Parent_BDT_Case_Age__c,Parent_COM_Case_Age__c,Parent_LSR_Case_Age__c, Parent_PDT_Case_Age__c   from case where id in :ParentIds ]);
       // Map<ID,Case> ResultMap = new Map <ID,Case> ();
        AggregateResult[] groupedResults = [select max(ParentID) ParentID, sum(BDT_Case_Age__c)childsum1, sum(COM_Case_Age__c) childsum2, sum(PDT_Case_Age__c) childsum3, sum(LSR_Case_Age__c) childsum4 from case  where parentid in :ParentIds or Id in:ParentIds];    
        for (AggregateResult Ar : groupedResults)
         {
           ID resultID = (ID)Ar.get('ParentID');
           case cupd = ResultMap.get(resultID);
           cupd.Parent_BDT_Case_Age__c = (Decimal) Ar.get('childsum1');
         cupd.Parent_COM_Case_Age__c = (Decimal) Ar.get('childsum2');            
           cupd.Parent_PDT_Case_Age__c = (Decimal) Ar.get('childsum3');
           cupd.Parent_LSR_Case_Age__c = (Decimal) Ar.get('childsum4');
           //Resultmap.remove(resultID);
           ResultMap.put(resultID,cupd); 
           system.debug('$$$$$$' + ResultMap.values());  
           system.debug('^^^^'+cupd.Parent_BDT_Case_Age__c);  
        }   
      
              
  
        if(ResultMap.values().size() > 0)
        {  
           List <Case> CasestobeUpdated = new List <Case>();
            for (Case cu : [Select id, Parent_BDT_Case_Age__c,Parent_COM_Case_Age__c,Parent_LSR_Case_Age__c, Parent_PDT_Case_Age__c   from case where id in :ResultMap.keySet()]  )
            {
                cu.Parent_BDT_Case_Age__c = ResultMap.get(cu.id).Parent_BDT_Case_Age__c;
                cu.Parent_COM_Case_Age__c = ResultMap.get(cu.id).Parent_COM_Case_Age__c;
                cu.Parent_PDT_Case_Age__c = ResultMap.get(cu.id).Parent_PDT_Case_Age__c;
                cu.Parent_LSR_Case_Age__c = ResultMap.get(cu.id).Parent_LSR_Case_Age__c;
                CasestobeUpdated.add(cu);
            }
           
            update CasestobeUpdated;
         
         //  upsert ResultMap.values() ID ;
         //   system.debug('$$$$$ '+ResultMap.size());
        }   

    }
} 

//

Test class 

@isTest(SeeAllData=true)
 public class ParentCaseGBOAgeTest{
 public static testmethod void casemethod()
 {
 
 Id caseRecordTypeId = Schema.Sobjecttype.case.getRecordTypeInfosByName().get('GBOCase').getRecordTypeId();
 List<Case> caseList=new List<Case>();
 
 Case ParentCase=new Case(status = 'Closed',
 Origin='Phone',
 
 RecordTypeId=caseRecordTypeId,
 Parent_BDT_Case_Age__c=12345.0000,
 Parent_COM_Case_Age__c=20000.0000,
 Parent_LSR_Case_Age__c=30000.0000,
 Parent_PDT_Case_Age__c=25000.0000,
 To__c='Business Data Team Incoming');
 caseList.add(ParentCase);
 
 Case Objcase1=new Case(status = 'Closed',
 Origin='Phone',
 
 RecordTypeId=caseRecordTypeId,
 Parent_BDT_Case_Age__c=12345.0000,
 Parent_COM_Case_Age__c=20000.0000,
 Parent_LSR_Case_Age__c=30000.0000,
 Parent_PDT_Case_Age__c=25000.0000,
 To__c='COM');
 caseList.add(Objcase1);
 
 Case Objcase2=new Case(status = 'Closed',
 Origin='Phone',
 
 RecordTypeId=caseRecordTypeId,
 Parent_BDT_Case_Age__c=12345.0000,
 Parent_COM_Case_Age__c=20000.0000,
 Parent_LSR_Case_Age__c=30000.0000,
 Parent_PDT_Case_Age__c=25000.0000,
 To__c='Product Data Team Incoming');
 caseList.add(Objcase2);
 
 Case Objcase3=new Case(status = 'Closed',
 Origin='Phone',
 
 RecordTypeId=caseRecordTypeId,
 Parent_BDT_Case_Age__c=12345.0000,
 Parent_COM_Case_Age__c=20000.0000,
 Parent_LSR_Case_Age__c=30000.0000,
 Parent_PDT_Case_Age__c=25000.0000,
 To__c='Customer Support');
 caseList.add(Objcase3);
 //Insert ParentCase;
 
         try{
        insert caseList;
        }
        Catch(Exception E)
        {}
        
        }
        
   }


.//in our test class it's not covered// please help me .how to I can fix this issue
User-added image

 
Martijn SchwarzerMartijn Schwarzer
Hi Viswa,

By default, your test code does not have access to the data in your org. So, you are correct to create your own testdata for running the test.

The fact that your code works fine if you add (seealldata=true), means that you are missing data in your testrun. From the error, it seems that there is another trigger called. That trigger is trying to retrieve data from the database, which is not available.

So you will also have to create the data needed by the other trigger in your testclass. Given the name of the trigger (emailtoQueueonCaseOwnerchange) you will probably have to create queues and/or queue members. But that's hard to determine without knowing the code of that trigger.

Hope this helps!

Best regards,
Martijn Schwärzer

Ps. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.