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
MalakondaiahMalakondaiah 

How to write the test class for below trigger?

Hi All,

i Have written the test class for below trigger, i have inserted the lead object records but i am unable to cover the if loop condition.

Please could you guide me.


trigger priceCalcLead on Lead (before Insert, before Update) 

    {  
        
        
         Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'test' and SobjectType='Lead'].Id;
        
        
        for(Lead led : Trigger.new)
        {              
                             
            if(led.RecordTypeId == recordTypeId){ 
           
                   if(led.Age__c >= 0 && led.Age__c <= 20){
                   
                              Decimal cost  = 45.20 ;
                              led.currencyfield__c = cost;  // currency field
                              
                              led.picklistfield1__c = 'pick1';
                              
                    
                              } 
                                  else if(led.Age__c >=21 && led.Age__c <=65) {
                                         
                                          
                                         Decimal cost2  = 49.20;
                                         led.currencyfield__c = cost2; 
                              
                                          led.picklistfield1__c = 'test2';
                              
                                          
                                          }
                                               
                                           else if(led.Age__c >=66 && led.Age__c <=100) {            
                                           Decimal cost3  = 65.00;
                                           led.currencyfield__c = cost3; 
                              
                                          led.picklistfield1__c = 'test2';
                                           }                                  
                                         
                                         
                                         else {
                                         Decimal cost4  =Null;
                                         led.currencyfield__c = cost4; 
                              
                                          led.picklistfield1__c = 'test2';
                                          }
                                          } 
}
sfdcMonkey.comsfdcMonkey.com
Hi use below test class :
@isTest
private class ConTest_Test
{
    static testMethod void TestLead()
    { 
	  Id RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('test').getRecordTypeId();
       lead oLead = new lead();
	   oLead.LastName = 'Test';
	   oLead.Company = 'Test';
	   oLead.Status = 'Open - Not Contacted';
	   oLead.recordtypeid = RecordTypeId;
	   oLead.Age__c = 10;
	   
	   // add other required fields of lead here
	   insert oLead;
	   
	  oLead.Age__c = 25;
       update oLead	 ;
	   
	   oLead.Age__c = 85;
       update oLead	 ;

	   oLead.Age__c = 102;
       update oLead	 ;
	   
    }
}

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks
sfdcmonkey.com