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
DaGunsterDaGunster 

Testing - here is the code - what more do you want to deploy?

Pardon me please. I am just totally perplexed about the testing. It's required , but hardly explained.  Here is my code.
 
What and how should the test be done so I can get this deployed today !
 
trigger MakeRouteEvents on Account (before insert, before update)
{
// Create routes for FAR's based on choices in Accounts.
// DECLARATIONS: OLD VALUES IN RECORD vs NEW VALUES IN RECORD.
Account[] oldRecords; // GET THE OLD VALUES
Account[] newRecords; // GET THE NEW VALUES
// DECLARATIONS: CONVERSIONS FROM THE FORM TO DATATYPES NEEDED BY EVENT.
Date VisitStartDate; // From Form: Date from date picker
String strVisitStartDate; // Visit Start Date - just date portion - converted to a string.
DateTime finalVisitStartDate; // This is the one written to the field 'RecurrenceStartDateTime'
String strVisitTime; // The time of day from the VISIT TIME picklist
String strDuration; // Duration of visit from Picklist
Integer intDuration; // Duration of site visit in minutes - converted to Integer
String strFrequency; // How often should the visit occur - form Picklist
Integer intFrequency; // How often should the visit occur - converted to integer.
String strVisitDayOfWeek; // Visit day of week, from Picklist.
Integer intVisitDayOfWeek; // Visit day of week, Mon, Tues: converted to Integer
//DateTime timeOfVisit; // converted to time for the Event table.
// DECLARATIONS: OTHER VARIABLES NEEDED BY EVENT
String strAccountId; // From Screen. Get The Account Id
String strUserId; // From API.
String strUserTimeZone; // From User Table. NEEDED BY EVENT.
Event[] existingEvents; // From User Table GET EXISTING ROUTE EVENTS FOR ACCOUNT SO WE CAN DELETE THEM.
// =================================================================
// START PROCESSING
// =================================================================
// GET THE OLD RECORD VALUES AND THE NEW RECORD VALUES
oldRecords = Trigger.old; // old trigger has old record values
newRecords= Trigger.new; // new trigger has new record values
Boolean WorkToDo; // Flag indicates if we have work to do, yes or no.
// DO WE HAVE ANYTHING TO DO?
WorkToDo = FALSE;
if (oldRecords[0].Visit_Start_Date__c != newRecords[0].Visit_Start_Date__c){WorkToDo = TRUE;}
else if (oldRecords[0].Visit_Day_of_Wk__c != newRecords[0].Visit_Day_of_Wk__c){WorkToDo = TRUE;}
else if (oldRecords[0].Visit_Time__c != newRecords[0].Visit_Time__c){WorkToDo = TRUE;}
else if (oldRecords[0].Visit_Wk__c != newRecords[0].Visit_Wk__c){WorkToDo = TRUE;}
else if (oldRecords[0].Visit_Wk_Freq__c != newRecords[0].Visit_Wk_Freq__c){WorkToDo = TRUE;}

// WHAT WOULD CAUSE US TO NOT WANT TO WORK?
if (newRecords[0].Visit_Start_Date__c == null) {WorkToDo = FALSE;}
System.debug('Work To do ' + WorkToDo);
if (WorkTodo)
{
// SCRAPE THE FORM AND SET VARIABLE VALUES
strAccountId = newRecords[0].Id; // Account
VisitStartDate = newRecords[0].Visit_Start_Date__c; // Visit Starting Date
strVisitTime = newRecords[0].Visit_Time__c;
strFrequency = newRecords[0].Visit_Wk_Freq__c; // Frequency
strDuration = newRecords[0].Visit_Duration__c; // Duration of Visit
strVisitDayOfWeek = newRecords[0].Visit_Day_of_Wk__c;
//CONVERT VISIT DAY OF WEEK TO RECURRENCE DAY OF WEEK MASK
System.debug('Day Of Week ' + strVisitDayOfWeek);
if (strVisitDayOfWeek == 'Sunday') {intVisitDayOfWeek = 1;}
else if (strVisitDayOfWeek == 'Monday') {intVisitDayOfWeek = 2;}
else if (strVisitDayOfWeek == 'Tuesday') {intVisitDayOfWeek = 4;}
else if (strVisitDayOfWeek == 'Wednesday') {intVisitDayOfWeek = 8;}
else if (strVisitDayOfWeek == 'Thursday') {intVisitDayOfWeek = 16;}
else if (strVisitDayOfWeek == 'Friday') {intVisitDayOfWeek = 32;}
else if (strVisitDayOfWeek == 'Saturday') {intVisitDayOfWeek = 64;}
// CONVERT DURATION OF VISIT FROM STRING TO INTEGER
if (strDuration == '15 Minutes') {intDuration = 15;}
else if (strDuration == '30 Minutes') {intDuration = 30;}
else if (strDuration == '45 Minutes') {intDuration = 45;}
else if (strDuration == '60 Minutes') {intDuration = 60;}
// CONVERT THE RECURRENCE INTERVAL FROM STRING TO INTEGER.
System.debug('strFrequency ' + strFrequency);
if (strFrequency == 'Weekly') {intFrequency = 1;}
else if (strFrequency == 'Two Weeks') {intFrequency = 2;}
else if (strFrequency == 'Three Weeks') {intFrequency = 3;}
else if (strFrequency == 'Four Weeks') {intFrequency = 4;}
else if (strFrequency == 'Six Weeks') {intFrequency = 6;}
else if (strFrequency == 'Eight Weeks') {intFrequency = 8;}
// CONVERT TIME OF DAY FROM STRING TO DATE
if (strVisitTime == '7:00 AM') {strVisitTime = '07:00';}
else if (strVisitTime == '7:30 AM') {strVisitTime = '07:30:00';}
else if (strVisitTime == '8:00 AM') {strVisitTime = '08:00:00';}
else if (strVisitTime == '8:30 AM') {strVisitTime = '08:30:00';}
else if (strVisitTime == '9:00 AM') {strVisitTime = '09:00:00';}
else if (strVisitTime == '9:30 AM') {strVisitTime = '09:30:00';}
else if (strVisitTime == '10:00 AM') {strVisitTime = '10:00:00';}
else if (strVisitTime == '10:30 AM') {strVisitTime = '10:30:00';}
else if (strVisitTime == '11:00 AM') {strVisitTime = '11:00:00';}
else if (strVisitTime == '11:30 AM') {strVisitTime = '11:30:00';}
else if (strVisitTime == '12 NOON') {strVisitTime = '12:00:00';}
else if (strVisitTime == '12:30 PM') {strVisitTime = '12:30:00';}
else if (strVisitTime == '1:00 PM') {strVisitTime = '13:00:00';}
else if (strVisitTime == '1:30 PM') {strVisitTime = '13:30:00';}
else if (strVisitTime == '2:00 PM') {strVisitTime = '14:00:00';}
else if (strVisitTime == '2:30 PM') {strVisitTime = '14:30:00';}
else if (strVisitTime == '3:00 PM') {strVisitTime = '15:00:00';}
else if (strVisitTime == '3:30 PM') {strVisitTime = '15:30:00';}
else if (strVisitTime == '4:00 PM') {strVisitTime = '16:00:00';}
else if (strVisitTime == '4:30 PM') {strVisitTime = '16:30:00';}
else if (strVisitTime == '5:00 PM') {strVisitTime = '17:00:00';}
else if (strVisitTime == '5:30 PM') {strVisitTime = '17:30:00';}
else if (strVisitTime == '6:00 PM') {strVisitTime = '18:00:00';}
// GET THE USER TIMEZONE
strUserId = UserInfo.getUserId(); // First, get the User ID
//System.debug('User Id is ' + strUserId);
strUserTimeZone = [SELECT TimeZoneSidKey from User where Id = :strUserId][0].TimeZoneSidKey;
//System.debug('Time Zone is ' + strUserTimeZone);
// ASSEMBLE DATES
System.debug('Now ' + System.Now());
System.debug('First Event Date is ' + VisitStartDate);
//System.debug(' Visit Start Date ' + newRecords[0].Visit_Start_Date__c);
strVisitStartDate = String.valueOf(VisitStartDate).substring(0,10); // get date portion, not time portion
System.debug('strVisitTime ' + strVisitTime);
strVisitStartDate = strVisitStartDate + ' ' + strVisitTime;
system.debug('strVisitStartDate ' + strvisitStartDate);
finalVisitStartDate = DateTime.valueOf(strVisitStartDate);
//strVisitStartDate = String.valueOf(VisitStartDate); // get date portion, not time portion
// EVERYTHING IS SET SO DROP EXISTING EVENTS AND CREATE NEW SCHEDULE OF EVENTS
existingEvents = [select Id, AccountId, isRecurrence from Event where AccountId = :strAccountId and isRecurrence = TRUE];
delete existingEvents;
Event newEvent = new Event
(
WhatId = strAccountId,
Subject = 'Route',
Location = 'On-Site',
Description = 'Scheduled Route Site Visit',
ShowAs = 'Busy',
IsReminderSet = FALSE,
IsRecurrence = TRUE,
DurationInMinutes = intDuration,
ActivityDate = date.today(),
ActivityDateTime = System.now(),
RecurrenceDayOfWeekMask = intVisitDayOfWeek,
RecurrenceInterval = intFrequency,
RecurrenceType = 'RecursWeekly',
//RecurrenceStartDateTime = strVisitStartDate,
RecurrenceStartDateTime = finalVisitStartDate,
RecurrenceEndDateOnly = Date.valueOf('2009-03-30'),
RecurrenceTimeZoneSidKey = strUserTimeZone
//ActivityDateTime = '9pm'
);
insert newEvent;

}

}
Rob P12Rob P12
    Force.com Cook book page 80 is an excellent starting point. Take a deep breath and review this material.
jeremyfrey1jeremyfrey1
Here's a few pointers:

1. Write a few statements to set up a mini-environment, holding any accounts, opportunities, events and custom objects you want to perform your tests on.  Since tests are transactional and are all rolled back, do not be afraid of writing code that creates records used solely for testing.  This is your "setup".  Many unit testing packages refer to these as TestSetup methods (and provide additional TestTearDown methods).

2. Write statements that will mimic the user actions your trigger is going to fire in response to.  In your case, it looks as though your trigger will fire when a user changes the "Visit_Start_Date__c" field.  This is just based off of my cursory glance at your code, there may be other field updates which would cause your trigger to execute.

3. Use assert statements to ensure your trigger performed as intended

Your basic unit test method will follow this pattern:

a. Set up environment

b. Write code to perform any field field updates / user actions which you assume will fire the trigger

c. Use System.assertEquals to ensure your trigger has, in fact, fired, and has produced the intended results

In your case, your unit test would be grabbing (or creating) an account, updating the Visit_Start_Date__c field, and ensuring that an Event was created with the correct values.

If you don't want to pull your hair out trying to test out all possible branches of your trigger (which may get extensive considering all the if / elseif statements you have), you should consider offloading some of the logic into a separate class.  This way, you can test that class separately, without worrying about any governor execution problems, which would arise if you're trying to test all permutations of the day of week, frequency, and visit time.  It will also make your code more maintainable and organized.  Consider this instead of your if/elses:

Code:
if (strVisitDayOfWeek == 'Sunday') {intVisitDayOfWeek = 1;}
else if (strVisitDayOfWeek == 'Monday') {intVisitDayOfWeek = 2;}
else if (strVisitDayOfWeek == 'Tuesday') {intVisitDayOfWeek = 4;}
else if (strVisitDayOfWeek == 'Wednesday') {intVisitDayOfWeek = 8;}
else if (strVisitDayOfWeek == 'Thursday') {intVisitDayOfWeek = 16;}
else if (strVisitDayOfWeek == 'Friday') {intVisitDayOfWeek = 32;}
else if (strVisitDayOfWeek == 'Saturday') {intVisitDayOfWeek = 64;}
// CONVERT DURATION OF VISIT FROM STRING TO INTEGER
if (strDuration == '15 Minutes') {intDuration = 15;}
else if (strDuration == '30 Minutes') {intDuration = 30;}
else if (strDuration == '45 Minutes') {intDuration = 45;}
else if (strDuration == '60 Minutes') {intDuration = 60;}

 becomes:

Code:
intVisitDayOfWeek = MyClass.FindDayOfWeekNumber(strVisitDayOfWeek);
intDuration = MyClass.ConvertDurationToInteger(intDuration);

- jer