FollowUpTaskTester.apex

Image:cookbook.jpg

Force.com Cookbook Sample Code
Chapter 4: Best Practices for Writing Apex - Controlling Recursive Triggers

To download all the code samples, access the Cookbook. - Image:Key_icon.gif

This Apex trigger is part of an example that shows how to control trigger recursion by using static member variables.

For this example to work properly, you first must define a custom checkbox field on Task. In this example, this field is named Create_Follow_Up_Task__c.

public class FollowUpTaskTester {

	private static integer NUMBER_TO_CREATE = 4;
	private static String UNIQUE_SUBJECT = 'Testing follow-up tasks';

	static testMethod void testCreateFollowUpTasks() {
		List<Task> tasksToCreate = new List<Task>();
		for (Integer i = 0; i < NUMBER_TO_CREATE; i++) {
			Task newTask = new Task(subject = UNIQUE_SUBJECT, ActivityDate = System.today(), Create_Follow_Up_Task__c = true );
			System.assert(newTask.Create_Follow_Up_Task__c);
			tasksToCreate.add(newTask);
		}

		insert tasksToCreate;
		System.assertEquals(NUMBER_TO_CREATE,[select count() from Task where subject = :UNIQUE_SUBJECT and ActivityDate = :System.today()]);

		// Make sure there are follow-up tasks created
		System.assertEquals(NUMBER_TO_CREATE, [select count() from Task where subject = :FollowUpTaskHelper.getFollowUpSubject(UNIQUE_SUBJECT) and ActivityDate = :System.today()+1]);
	}

	static testMethod void assertNormalTasksArentFollowedUp() {
		List<Task> tasksToCreate = new List<Task>();
		for (integer i = 0; i < NUMBER_TO_CREATE; i++) {
			Task newTask = new Task(subject=UNIQUE_SUBJECT, ActivityDate = System.today(), Create_Follow_Up_Task__c = false);
			tasksToCreate.add(newTask);
		}

		insert tasksToCreate;
		System.assertEquals(NUMBER_TO_CREATE, [select count() from Task where subject=:UNIQUE_SUBJECT and ActivityDate =:System.today()]);

		// There should be no follow-up tasks created
		System.assertEquals(0, [select count() from Task where subject=:FollowUpTaskHelper.getFollowUpSubject(UNIQUE_SUBJECT) and ActivityDate =:(System.today() +1)]);
	}
}

Sample code provided by salesforce.com. All rights reserved.