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
beenerbeener 

Apex trigger & test help please

Hi,
I've written a very simple Apex Trigger (My first).
it aims to count the number of Timesheet__c records with a given date, and update these to another object called Week__c.

I cannot seem to deploy this trigger. due to Test coverage being insufficiet (0%). and due to average Apex test coverage, which i is 50% (as opposed to the required 75%).
  1. Please help me understand what would be a test method for this trigger.
  2. Where would I write this method (in the apex trigger code? after the closing bracket?


Code:
trigger Timesheets_Count on Week__c (after insert, after update, after delete, after undelete) {
 Date weekending = Trigger.new[0].Week_Ending__c;
 Week__c my_week = [select w.id, w.Number_Of_Timesheets__c from Week__c w WHERE w.Week_Ending__c = :weekending limit 1];
 Integer number_of_sheets = [select COUNT() from Timesheet__c t where t.Week_Ending__c =:weekending];
 my_week.Number_Of_Timesheets__c = number_of_sheets;
 //update My_week;// is this Line needed at all
 }

Thanks so much.

Ben

aalbertaalbert
Test Methods needed to be written in an Apex Class. So you could create an apex class that contains the test methods. And the test methods would create any necessary test data, and then either through an update, insert, or delete DML operation, cause your trigger to initiate. And you can create multiple test methods to test the different scenarios of your trigger's logic.

Then during deployment, include the trigger and the apex class (that contains the test methods).


beenerbeener
Thanks. I'll try that.