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

Put your hands together


Now that we know about triggers and classes, it's time to revisit our business requirement again and complete the process to synchronize the phone field on contacts with accounts. We'll start by taking our outer class shell and inserting our updateContactPhones() method from Chapter 3, More and Later, as shown:

public with sharing class accountMethods {
  public static void updateContactPhones(
  List<Account> priorVersions, 
  List<Account> updatedVersions
  ){
    Set<Id> modifiedAccounts_Ids = new Set<Id>();
    for ( Integer i=0;i<updatedVersions.size();i++ ){
      if ( updatedVersions[i].Phone != priorVersions[i].Phone ){
        modifiedAccounts_Ids.add(updatedVersions[i].Id);
      }
    }
    if ( modifiedAccounts_Ids.size() > 0 ){
      List<Contact> contactQuery = [
      Select Id, Phone, AccountId, Account.Phone 
      from Contact
      where AccountId in :modifiedAccounts_Ids 
      limit :( 
      limits.getLimitQueryRows...