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
SrinuSrinu 

Test Coverage Error for an Simple ' before update' trigger

Hi All,

 

  Here is my trigger, and I have got an error while writing the test class.

  

trigger UpdateRating on Lead (before update) {
    for(Lead l:Trigger.new)
    {
        if(l.Pardot_Rating__c !=null && l.Pardot_Rating__c =='A')
        {
         l.Rating = 'A';
        }
       else if(l.Pardot_Rating__c !=null && l.Pardot_Rating__c =='B')
        {
         l.Rating ='B';
        }
       else if(l.Pardot_Rating__c !=null && l.Pardot_Rating__c =='C')
        {
         l.Rating = 'C';
        }
     }
}

    Here is the Test Class.

   

@isTest(seeAllData = true)
Private Class TestUpdateRatingTrigger{
 @isTest
 Private Static Void testUpdateRatingTrigger(){
     Test.StartTest();
     Lead testLead = new Lead(
       FirstName = 'TestFirstName',
       LastName = 'TestLastName',
       Email = 'Srinu.mandalapu@stratus.com',
       Phone = '4055090045',
       Prospect_Type__c = 'Direct End User',       
       Title = 'Test Title',
       Company = 'ABC',      
       Status = 'Open',
       Rating = 'A'     
     );
     insert testLead;
     testLead.Rating  = 'B';
     update testLead;
     //verifying updation
     Lead updatedLead = [SELECT Rating FROM Lead WHERE Id = :testLead.Id];
     System.assertEquals('B', updatedLead.Rating);     
     Test.StopTest();
    
 }
}

   Error:

   

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, jigsaw_clean.crmLastModifiedLead: execution of BeforeInsert caused by: line -1, column -1: Previous load of class failed: knowledgecontroller: []

Class.TestUpdateRatingTrigger.testUpdateRatingTrigger: line 17, column 1

   Here "KnowledgeController" is the controller in my org.

 

  Is "knowledgeController" managed package in my org? I saw the packages in my ORG, I did not find any package under that name...

 

  Any help would be greatly aprreciated...

SFDC Admin & AnalystSFDC Admin & Analyst

Try changing to this

 

System.assertEquals('A', updatedLead.Rating);
Navatar_DbSupNavatar_DbSup

Hi,

 

I have tried your trigger in my org and its working. I have written the test method for the same but not getting any error.

 

Try the below test method code as reference:

 

 

@isTest(seeAllData = true)

Private Class TestUpdateRatingTrigger{

 @isTest

 Private Static Void testUpdateRatingTrigger(){

     Test.StartTest();

     Lead testLead = new Lead(

       FirstName = 'TestFirstName',

       LastName = 'TestLastName',

       Email = 'Srinu.mandalapu@stratus.com',

       Phone = '4055090045',

      // Prospect_Type__c = 'Direct End User',      

       Title = 'Test Title',

       Company = 'ABC',  

       Pardot_Rating__c='A',  

       Status = 'Open',

       Rating = 'A'    

     );

     insert testLead;

     testLead.Rating  = 'A';

     update testLead;

     Lead testLead1 = new Lead(

       FirstName = 'TestFirstName',

       LastName = 'TestLastName',

       Email = 'Srinu.mandalapu@stratus.com',

       Phone = '4055090045',

      // Prospect_Type__c = 'Direct End User',      

       Title = 'Test Title',

       Company = 'ABC',  

       Pardot_Rating__c='B',  

       Status = 'Open',

       Rating = 'B'    

     );

      insert testLead1;

     testLead1.Pardot_Rating__c='B';

     update testLead1;

     Lead testLead2 = new Lead(

       FirstName = 'TestFirstName',

       LastName = 'TestLastName',

       Email = 'Srinu.mandalapu@stratus.com',

       Phone = '4055090045',

      // Prospect_Type__c = 'Direct End User',      

       Title = 'Test Title',

       Company = 'ABC',  

       Pardot_Rating__c='C',  

       Status = 'Open',

       Rating = 'C'    

     );

      insert testLead2;

     testLead2.Pardot_Rating__c='C';

     update testLead2;

    

   

 }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.