MassUpdateContactsHelper.apex

Image:cookbook.jpg

Force.com Cookbook Sample Code
Chapter 4: Best Practices for Writing Apex - Avoiding Apex Governor Limits

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

This Apex trigger is part of an example that demonstrates several methods for avoiding execution governor limits, including:

  • Making sure that database statements such as insert(), update(), and delete() operate in bulk
  • Using Limits.getDMLRows() for error handling
  • Limiting queries by using static class variables to store query results
  • Testing bulk behavior
public class MassUpdateContactsHelper {

	//Static variables are local to the context of a Web request
	//or testMethod during a runTests call.
	//Therefore, we can set this static variable in the before
	//trigger when we validate, and insert the proporsed changes
	//during the after trigger

	private static List<Contact> contactsToRemember = null;

	public static void rememberContactsForUpdate(List<Contact> contacts) {

		contactsToRemember = contacts;
	}

	public static List<Contact> getContactsForUpdate() {

		return contactsToRemember;
	}
}

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