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
MarniMarni 

How to test Apex Trigger

I am new to Apex and am having an issue moving a trigger I created in Sandbox through a Change Set to production.  The trigger I created is working great but when I try the Change Set it does not validate because it says I need to test the trigger.  I was reading about Apex Test Methods but am a little confused as to how to I execute the test.  Any help on next steps would be greatly appreciated!  The trigger I need to test is:

 

trigger CoverageLetterTask on Claim__c (before update) {
 List<Task> tasks = new List<Task>();
    for(Claim__c claim : Trigger.new){
    if(claim.Task_created__c != null &&
       claim.Task_created__c == false &&
       claim.Approval_Received__c == true &&
       claim.Insured__c != 'Catholic Health East'){


//Do your Task creation
            Task CoverageTask= new Task();
            CoverageTask.ActivityDate = claim.LVL_Received_Date__c + 30;   
            CoverageTask.WhatId = claim.id;
            CoverageTask.OwnerId = claim.Assigned_Examiner2__c;   
            CoverageTask.Priority = 'Normal';   
            CoverageTask.Status = 'Not Started';
            CoverageTask.Subject = claim.Program_ID__c + ' Coverage Letter -- ' + claim.Insured__C + ' -- ' + claim.Claimant_Name__c;   

//Add the Task
tasks.add(CoverageTask);
            
//Update the field on Claim so this isn't done again
            claim.Task_Created__c = true;
        }
    }  
    
    insert tasks;
}

Best Answer chosen by Admin (Salesforce Developers) 
JBabuJBabu

Hi,

 

You need to write test class which does the below operation like in your test class with test data you need to perform an insert on the claim__c object.

 

@isTest

private class TestCoverageLetterTask {

 

  private static testMethod void testCoverageLetterTaskMethod() {

   Test.StartTest();

   // Here you code to insert in to Claim__c object..

   Test.StopTest();

  }

 

}

All Answers

JBabuJBabu

Hi,

 

You need to write test class which does the below operation like in your test class with test data you need to perform an insert on the claim__c object.

 

@isTest

private class TestCoverageLetterTask {

 

  private static testMethod void testCoverageLetterTaskMethod() {

   Test.StartTest();

   // Here you code to insert in to Claim__c object..

   Test.StopTest();

  }

 

}

This was selected as the best answer
MarniMarni

That worked!  And I had multiple triggers on the Claim Object and was able to create multiple claim updates in the one class and got 100% Code Coverage!  Thanks for your help on this!