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
pinky1pinky1 

Trigger Test class, code coverage help

We have two custom fields on leads pardot hard bounced and pardot hard bounced cloned. if pardot hard bounced is true, we need to make pardot hard bounced cloned, if a lead updates a field on the record.

For this i have written Trigger and the code coverage is 0%. I want to deploy it to production. If i need to deploy  need to write a test class and apex class. Can someone help me with this. I am new to coding.

below is the trigger i have written:


trigger UpdateLead on Lead (before update) {
for(Lead l: Trigger.new) {
        If (l.pi__pardot_hard_bounced__c == true ){
           l.Pardot_Hard_Bounce_Cloned__c = true;
        }
        else {
        l.Pardot_Hard_Bounce_Cloned__c = False;
        }
    }
    }
Best Answer chosen by pinky1
Raj VakatiRaj Vakati
@isTest
private class UpdateLeadTest {
    static testMethod void leadTest1(){
    Lead leadOne = new Lead(Company = 'Test Lead',pi__pardot_hard_bounced__c  =True , 
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert leadOne;
	leadOne.pi__pardot_hard_bounced__c   =false ; 

	update leadOne l 
	 Lead leadTwo = new Lead(Company = 'Test Lead',pi__pardot_hard_bounced__c  =False , 
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert leadTwo;

leadTwo.pi__pardot_hard_bounced__c   =True ;
Update leadTwo ;
    }
}

 

All Answers

Raj VakatiRaj Vakati
Use this code
@isTest
private class UpdateLeadTest {
    static testMethod void leadTest1(){
    Lead leadOne = new Lead(Company = 'Test Lead',pi__pardot_hard_bounced__c  =True , 
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert leadOne;
	
	
	 Lead leadTwo = new Lead(Company = 'Test Lead',pi__pardot_hard_bounced__c  =False , 
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert leadTwo;
    }
}
Tejender Mohan 9Tejender Mohan 9

Hey

Consider Test class as an automated way to test all the scenario which your code functionality can face.
In this case, You can write a test class and

  • Create a Lead record in the test class.
  • Update its field value(TestLead.pi__pardot_hard_bounced__c = true) between Test.Start() and Test.Stop().
  • Add system assert to check Pardot_Hard_Bounce_Cloned__c value of your test lead.
To get 100% coverage, You will have to write methods on Test class which execute each line of your trigger code.
You can use the developer console to check which line has been covered by your test class after test execution.
As a best practice to learn, I would suggest you read a bit about the test class and write it on your own.
Your problem is easy, It won't be difficult for you to write a test class code for this.
Thanks
SarvaniSarvani
Hey Pinky1,

Try the test class below
@isTest 
public class UpdateLeadTestClass{
@isTest public static void  Method1(){
	//positive case
	Lead L=new Lead();
	L.LastName='User1';
	L.Company='Example Company';
	L.status='New';
	L.pi__pardot_hard_bounced__c= true;
	Insert L;
 
//negative case
	Lead L1=new Lead();
	L1.LastName='User1';
	L1.Company='Example Company';
	L1.status='New';
	L1.pi__pardot_hard_bounced__c= false;
	Insert L1;
	   
    try{
        Update L;
        Update L1;
       }
	catch(DmlException e) {
    System.debug('The following exception has occurred: ' + e.getMessage());
	}
  }
 }

Hope this helps,

Thanks,
Sarvani
Raj VakatiRaj Vakati
@isTest
private class UpdateLeadTest {
    static testMethod void leadTest1(){
    Lead leadOne = new Lead(Company = 'Test Lead',pi__pardot_hard_bounced__c  =True , 
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert leadOne;
	leadOne.pi__pardot_hard_bounced__c   =false ; 

	update leadOne l 
	 Lead leadTwo = new Lead(Company = 'Test Lead',pi__pardot_hard_bounced__c  =False , 
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert leadTwo;

leadTwo.pi__pardot_hard_bounced__c   =True ;
Update leadTwo ;
    }
}

 
This was selected as the best answer
pinky1pinky1
Hi everyone,

Thanks for helping me with the code