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
NattyForceNattyForce 

Help writing test method

I'm completely new to Apex Code and OOP in general. I have written two simple, and probably poorly formed, triggers to update/clear some fields based on certain criteria. The triggers are working as expected in the sandbox but I cannot deploy without test methods. Any help/assisstance would be greatly appreciated.

 

I've looked at a few other threads about test methods but cannot for the life of me wrap my head around them.

 

Trigger #1:

trigger disagreeCompliance on CTM__c (before insert, before update) {

 for(CTM__c ctm : trigger.new){
  if(ctm.Disagree_with_Root_Cause_Checkbox__c != FALSE && ctm.Verified_By_1__c == null){
   ctm.Verified_Process_Improvement_Step_1__c = null;
   ctm.Verified_Responsible_Party_1__c = null;
   ctm.Verified_Completion_Date_1__c = null;
  }
  if(ctm.Disagree_with_Root_Cause_Checkbox__c != FALSE && ctm.Verified_By_2__c == null){
   ctm.Verified_Process_Improvement_Step_2__c = null;
   ctm.Verified_Responsible_Party_2__c = null;
   ctm.Verified_Completion_Date_2__c = null;
  }
  if(ctm.Disagree_with_Root_Cause_Checkbox__c != FALSE && ctm.Verified_By_3__c == null){
   ctm.Verified_Process_Improvement_Step_3__c = null;
   ctm.Verified_Responsible_Party_3__c = null;
   ctm.Verified_Completion_Date_3__c = null;
  }
  if(ctm.Disagree_with_Root_Cause_Checkbox__c != FALSE && ctm.Verified_By_4__c == null){
   ctm.Verified_Process_Improvement_Step_4__c = null;
   ctm.Verified_Responsible_Party_4__c = null;
   ctm.Verified_Completion_Date_4__c = null;
  }
  if(ctm.Disagree_with_Root_Cause_Checkbox__c != FALSE && ctm.Verified_By_5__c == null){
   ctm.Verified_Process_Improvement_Step_5__c = null;
   ctm.Verified_Responsible_Party_5__c = null;
   ctm.Verified_Completion_Date_5__c = null;
  }
 }
}



Trigger #2:

trigger complianceVerified on CTM__c (before insert, before update) {

 for(CTM__c ctm : trigger.new){
  if(ctm.Process_Improvement_Step_1__c != null && ctm.Disagree_with_Root_Cause_Checkbox__c != TRUE){
   ctm.Verified_Process_Improvement_Step_1__c = ctm.Process_Improvement_Step_1__c;
   ctm.Verified_Responsible_Party_1__c = ctm.Process_Improvement_Responsible_Party_1__c;
   ctm.Verified_Completion_Date_1__c = ctm.Process_Improvement_Completion_Date_1__c;
  }
  if(ctm.Process_Improvement_Step_2__c != null && ctm.Disagree_with_Root_Cause_Checkbox__c != TRUE){
   ctm.Verified_Process_Improvement_Step_2__c = ctm.Process_Improvement_Step_2__c;
   ctm.Verified_Responsible_Party_2__c = ctm.Process_Improvement_Responsible_Party_2__c;
   ctm.Verified_Completion_Date_2__c = ctm.Process_Improvement_Completion_Date_2__c;
  }
  if(ctm.Process_Improvement_Step_3__c != null && ctm.Disagree_with_Root_Cause_Checkbox__c != TRUE){
   ctm.Verified_Process_Improvement_Step_3__c = ctm.Process_Improvement_Step_3__c;
   ctm.Verified_Responsible_Party_3__c = ctm.Process_Improvement_Responsible_Party_3__c;
   ctm.Verified_Completion_Date_3__c = ctm.Process_Improvement_Completion_Date_3__c;
  }
  if(ctm.Process_Improvement_Step_4__c != null && ctm.Disagree_with_Root_Cause_Checkbox__c != TRUE){
   ctm.Verified_Process_Improvement_Step_4__c = ctm.Process_Improvement_Step_4__c;
   ctm.Verified_Responsible_Party_4__c = ctm.Process_Improvement_Responsible_Party_4__c;
   ctm.Verified_Completion_Date_4__c = ctm.Process_Improvement_Completion_Date_4__c;
  }
  if(ctm.Process_Improvement_Step_5__c != null && ctm.Disagree_with_Root_Cause_Checkbox__c != TRUE){
   ctm.Verified_Process_Improvement_Step_5__c = ctm.Process_Improvement_Step_5__c;
   ctm.Verified_Responsible_Party_5__c = ctm.Process_Improvement_Responsible_Party_5__c;
   ctm.Verified_Completion_Date_5__c = ctm.Process_Improvement_Completion_Date_5__c;
  }
 }
}



Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Here is your test class

@isTest
private class testClass_Trigger{

    private static TestMethod void testTrigger1(){
        
        //Step 1 : Data Insertion
        CTM__c ctm = new CTM__c();
        ctm.Disagree_with_Root_Cause_Checkbox__c  = true;
        //Fill other req field as well if thee are, but don't fill Verified_By_1__c,Verified_By_2__c,Verified_By_3__c ,Verified_By_4__c ,Verified_By_5__c    fields

        test.startTest();
        insert ctm;
        test.stopTest();
        
        
        
    }


private class testClass_Trigger{

    private static TestMethod void testTrigger2(){
        
        //Step 1 : Data Insertion
        CTM__c ctm = new CTM__c();
        ctm.Disagree_with_Root_Cause_Checkbox__c  = false;
        ctm.Process_Improvement_Step_1__c = 'testStep1';
        ctm.Process_Improvement_Step_2__c = 'testStep1';
        ctm.Process_Improvement_Step_3__c = 'testStep1';
        ctm.Process_Improvement_Step_4__c = 'testStep1';
        ctm.Process_Improvement_Step_5__c = 'testStep1';
        //Fill other req field as well if thee are

        test.startTest();
        insert ctm;
        test.stopTest();
        
        
        
    }
}

 I hope it will help you

All Answers

Shashikant SharmaShashikant Sharma

Here is your test class

@isTest
private class testClass_Trigger{

    private static TestMethod void testTrigger1(){
        
        //Step 1 : Data Insertion
        CTM__c ctm = new CTM__c();
        ctm.Disagree_with_Root_Cause_Checkbox__c  = true;
        //Fill other req field as well if thee are, but don't fill Verified_By_1__c,Verified_By_2__c,Verified_By_3__c ,Verified_By_4__c ,Verified_By_5__c    fields

        test.startTest();
        insert ctm;
        test.stopTest();
        
        
        
    }


private class testClass_Trigger{

    private static TestMethod void testTrigger2(){
        
        //Step 1 : Data Insertion
        CTM__c ctm = new CTM__c();
        ctm.Disagree_with_Root_Cause_Checkbox__c  = false;
        ctm.Process_Improvement_Step_1__c = 'testStep1';
        ctm.Process_Improvement_Step_2__c = 'testStep1';
        ctm.Process_Improvement_Step_3__c = 'testStep1';
        ctm.Process_Improvement_Step_4__c = 'testStep1';
        ctm.Process_Improvement_Step_5__c = 'testStep1';
        //Fill other req field as well if thee are

        test.startTest();
        insert ctm;
        test.stopTest();
        
        
        
    }
}

 I hope it will help you

This was selected as the best answer
NattyForceNattyForce

Shashikant Sharma thank you so much!! That worked like a charm and now I have 100% coverage. You are truly a life saver. If you're every in central FL I'll buy you a round of beers. :smileyvery-happy: 

 

I especially appreciate you commenting everything out like that.I aspire to be like you sir or madam.

 

Thanks again!!

 

-natty

Shashikant SharmaShashikant Sharma

Your welcome natty,

 

I am also here to learn, you can read some post in my blog  to structure test class

http://forceschool.blogspot.com/search/label/Test%20Apex