Book Image

Learning Apex Programming

5 (1)
Book Image

Learning Apex Programming

5 (1)

Overview of this book

Table of Contents (17 chapters)
Learning Apex Programming
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Where we're going, we don't need roads


In Chapter 2, Apex Limits, we looked at how to write a method in Apex that will update contact numbers when the phones on their account records are modified. Then, we modified our code to operate efficiently on just the records that need to be operated upon and also added in statements to ensure we don't exceed our Apex governor limits. However, the only way to stay under the limits was to cap the number of records we operated upon, possibly leaving some records untouched. While business requirements can vary, I think it's safe to assume that no one likes it when code works only sometimes. If you need a refresher, here is the method we wrote:

//A common pattern for triggered operations in Apex
public static void updateContactPhones(
List<Account> priorAccounts, 
List<Account> updatedAccounts
){
  Set<Id> modifiedAccounts_Ids = new Set<Id>();
  for ( Integer i=0;i<updatedAccounts.size();i++ ){
    if (updatedAccounts[i].Phone...