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
AntonyWarcAntonyWarc 

How to build a test calss for my trigger

I have no Apex coding experience, can someone help me write a test class for the following Apex Trigger?

 

trigger updateCCLookupField on Lead (before insert, before update) {

List<Lead> leads = new List<Lead>();

for (Lead l : Trigger.new)
{
Try
{
Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
l.CC_Lookup__c = AssociatedCC.Id;
}
Catch(Exception e)
{
l.CC_Lookup__c = 'a0zD00000028X59';
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Venkatesh.ax1803Venkatesh.ax1803

@isTest
private class AUMTriggerHelperTest {

// This is for insert
 static testMethod void testLeadMehod1() {

     // Create Custom object
     Campaign_Codes__c testccode = new Campaign_Codes__c();
    // Fill teh reuired fields
        insert   testccode ;
   // Create Lead
      
     Lead testLead = new Lead();
      testLead.Campaign_Code__c = testccode. CodeOnCC__c
     //fill the required fields
      insert testLead;     

     // System Assertion
      
System.assertEquals(testLead.CC_Lookup__c,testccode.id);
    

   }

   
 
   // For update

    static testMethod void testLeadMehod1() {

     
   // Create Lead
      
     Lead testLead = new Lead();
      
     //fill the required fields
      insert testLead;     
     
// Create Custom object
     Campaign_Codes__c testccode = new Campaign_Codes__c();
    // Fill teh reuired fields
        insert   testccode ;

      // update lead

    
      testLead.Campaign_Code__c = testccode. CodeOnCC__c;
    update  testLead;

     // System Assertion
      
System.assertEquals(testLead.CC_Lookup__c,testccode.id);

}

 

Similar;y test in bulk mode - multiple leads and custom objects update and insertion

 

positive test classes - If all values are there

negative test cases - if null values in the fields

 

 

All Answers

Venkatesh.ax1803Venkatesh.ax1803

@isTest
private class AUMTriggerHelperTest {

// This is for insert
 static testMethod void testLeadMehod1() {

     // Create Custom object
     Campaign_Codes__c testccode = new Campaign_Codes__c();
    // Fill teh reuired fields
        insert   testccode ;
   // Create Lead
      
     Lead testLead = new Lead();
      testLead.Campaign_Code__c = testccode. CodeOnCC__c
     //fill the required fields
      insert testLead;     

     // System Assertion
      
System.assertEquals(testLead.CC_Lookup__c,testccode.id);
    

   }

   
 
   // For update

    static testMethod void testLeadMehod1() {

     
   // Create Lead
      
     Lead testLead = new Lead();
      
     //fill the required fields
      insert testLead;     
     
// Create Custom object
     Campaign_Codes__c testccode = new Campaign_Codes__c();
    // Fill teh reuired fields
        insert   testccode ;

      // update lead

    
      testLead.Campaign_Code__c = testccode. CodeOnCC__c;
    update  testLead;

     // System Assertion
      
System.assertEquals(testLead.CC_Lookup__c,testccode.id);

}

 

Similar;y test in bulk mode - multiple leads and custom objects update and insertion

 

positive test classes - If all values are there

negative test cases - if null values in the fields

 

 

This was selected as the best answer
AntonyWarcAntonyWarc
Needed a bit of tweaking but got there in the end thanks!