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
Rajesh SFDCRajesh SFDC 

how to increase code coverage in apex trigger?

iam getting an error :could not run tests on class because invalid type:baseConfigController
apex trigger:
===============================
trigger test on Case (before update )
{
  Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();

  case caseold=trigger.old[0];
  For (Case cs : Trigger.new)
  {
    for (string fieldName : caseFields.keySet())
     {
       
        if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
        {
           // cs.Track_status_history__c = 'Field ' + fieldName + ' changed from ' + string.valueOf(Trigger.oldMap.get(cs.id).get(fieldName)) + ' to '  +string.valueOf(cs.get(fieldName));
             string oldvalue = string.valueOf(caseold.get(fieldName));
            
             string newvalue= string.valueOf(cs.get(fieldName));
           
             cs.Track_status_history__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue;
         
             break;
          }   
       }
  }
}
================================test class============================================
@isTest
public class Test_casehistory
{

    public static testmethod void m1()
    {
     Case cs=new case();
     cs.Origin='Web';
     cs.Reason='other';
     cs.Priority='Medium';
     cs.Status='New';
     cs.Type='Other';

     test.startTest();
     list<case> lTest1=new list<case>();
     lTest1 = [SELECT Id,Origin,Reason,Priority,Status,Type  FROM Case WHERE Id=:cs.Id];
     update lTest1;
     test.stopTest();
    }
}
ShashForceShashForce
Hi,

The case record you created in your test class should be inserted to fire the trigger. Please change yoiur code to something like this:

@isTest
public class Test_casehistory
{

    public static testmethod void m1()
    {
     Case cs=new case();
     cs.Origin='Web';
     cs.Reason='other';
     cs.Priority='Medium';
     cs.Status='New';
     cs.Type='Other';

     test.startTest();
     insert cs; //added line of code to insert cs
     list<case> lTest1=new list<case>();
     lTest1 = [SELECT Id,Origin,Reason,Priority,Status,Type  FROM Case WHERE Id=:cs.Id];
     update lTest1;
     test.stopTest();
    }
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Rajesh SFDCRajesh SFDC
still iam getting a same error
could not run tests on class 01p90000004Rgog because:line 30,column 39:invalid type:baseConfigcontroller

01p90000004Rgog means test class url id in dis test class, i dont have line no 30,, where s dat line i done no
ShashForceShashForce
Strange! I am able to run the same test class without any errors. Can you check if there are any blank spaces or characters? Also, please delete the test class and create again and check if you still face the error.
Rajesh SFDCRajesh SFDC
now cleared dat error but low per
trigger test on Case (before update )
{
  Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();

  case caseold=trigger.old[0];
  For (Case cs : Trigger.new)
  {
    for (string fieldName : caseFields.keySet())
     {
      
        if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
        {
           // cs.Track_status_history__c = 'Field ' + fieldName + ' changed from ' + string.valueOf(Trigger.oldMap.get(cs.id).get(fieldName)) + ' to '  +string.valueOf(cs.get(fieldName));
             string oldvalue = string.valueOf(caseold.get(fieldName)); --------------->dosnot execute its red line
           
             string newvalue= string.valueOf(cs.get(fieldName));---------------------> dosnot execute its red line
          
             cs.Track_status_history__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue;
        
             break;
          }  
       }
  }
}
ShashForceShashForce
Please try to update a list of 2-3 cases in your test class and check if the test coverage increases.
<Learning Apex<Learning Apex
Hi @ShashForce

Can you please help me with steps for pasting the code here in the format you posted ?
as Rajesh SFC has jut copy-pasted it.

Thank you in advance!