Create an Apex Class
Next, let’s create an Apex class to debug. This
class removes cold accounts from a list so that you can focus on more promising
clients.
- Create an Apex class called AccountViewerController.
-
Replace your class’s default contents with this code.
1public class AccountViewerController { 2 3 4 public Boolean removeCold { get; set; } 5 public List<Account> results { get; set; } 6 7 8 public AccountViewerController() { 9 10 11 removeCold = false; 12 13 14 results = [SELECT Id, Name, Owner.Name, Rating, BillingCity, BillingState 15 from Account 16 WHERE BillingCity = 'Suffragette City' 17 Order By Name ASC]; 18 } 19 20 21 public List<Account> getAccountTable() { 22 23 24 List<Account> accountsToReturn; 25 accountsToReturn = new List<Account>(); 26 accountsToReturn.addAll(results); 27 28 29 if (removeCold==true) { 30 removeColdAccounts(accountsToReturn); 31 } 32 33 34 return accountsToReturn; 35 } 36 37 38 public void removeColdAccounts(List<Account> listToReduce) { 39 40 41 System.debug('Removing "cold" accounts'); 42 System.debug(' size before: ' + listToReduce.size()); 43 44 45 for (Integer i = 0; i < listToReduce.size(); i++) { 46 Account a = listToReduce.get(i); 47 if (a.Rating.equalsIgnoreCase('Cold')) { 48 listToReduce.remove(i); 49 System.debug('removed cold account: ' + a.Name); 50 } 51 } 52 53 54 System.debug(' size after: ' + listToReduce.size()); 55 } 56 57 58 public void noOp() { 59 } 60 61 62} - Save AccountViewerController.cls.