Last active
February 9, 2020 14:38
-
-
Save salesforceBen/e551787aff400049a72f132bdee67288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GitHub repo link: https://github.com/salesforceBen/governorLimitsExercise | |
// Exercise 1 issue: | |
Task taskObject; | |
for (Contact contactObject : [SELECT Id, AccountId, LastName FROM Contact]) | |
{ | |
taskObject = new Task(WhoId = contactObject.Id); | |
insert taskObject; | |
} | |
// Exercise 1 fix: | |
List<Task> tasks = new List<Task>(); | |
Task taskObject; | |
for (Contact contactObject : [SELECT Id, AccountId, LastName FROM Contact]) | |
{ | |
taskObject = new Task(WhoId = contactObject.Id); | |
tasks.add(taskObject); | |
} | |
insert tasks; | |
// Exercise 2 issue: | |
for (Account accountObject : [SELECT Id, Description FROM Account]) | |
{ | |
Integer countOfEvents = [SELECT COUNT() FROM EVENT WHERE WhatId =: accountObject.Id]; | |
if (countOfEvents > 0) | |
{ | |
accountObject.Description = String.valueOf(countOfEvents); | |
} | |
update accountObject; | |
} | |
// Exercise 2 fix: | |
Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id FROM Account]); | |
Set<Id> accountsWithEvents = new Set<Id>(); | |
Map<String, String> eventsPerAccount = new Map<String, String>(); | |
List<Account> accountList = new List<Account>(); | |
for (Event eventObject : [SELECT Id, WhatId FROM Event WHERE WhatId IN : accountsMap.keySet()]) | |
{ | |
accountsWithEvents.add(eventObject.WhatId); | |
} | |
for (AggregateResult aggregate : [SELECT COUNT(Id) theId, WhatId theWhatId FROM Event WHERE WhatId IN :accountsWithEvents GROUP BY WhatId]) | |
{ | |
eventsPerAccount.put(String.valueOf(aggregate.get('theWhatId')), String.valueOf(aggregate.get('theId'))); | |
} | |
for (Account accountObject : [SELECT Id, Description FROM Account WHERE Id IN : eventsPerAccount.keySet()]) | |
{ | |
if (accountObject.Description != eventsPerAccount.get(String.valueOf(accountObject.Id))) | |
{ | |
accountObject.Description = eventsPerAccount.get(String.valueOf(accountObject.Id)); | |
accountList.add(accountObject); | |
} | |
} | |
if (accountList.size() > 0) | |
{ | |
update accountList; | |
} | |
// SOQL in a for loop issue: | |
List<Account> accounts = [SELECT Id, Name FROM Account]; | |
List<Integer> contactCount = new List<Integer>(); | |
for (Integer i = 0; i <= accounts.size(); i++) | |
{ | |
Integer count = [SELECT COUNT() FROM Contact WHERE AccountId IN : accounts ]; | |
contactCount.add(count); | |
} | |
// SOQL in a for loop fix: | |
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id FROM Account]); | |
Map<String, Integer> accountNamesAndRespectiveContactCounts = new Map<String, Integer>(); | |
for(Account accountObject : [SELECT Name, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountMap.keySet()]) | |
{ | |
accountNamesAndRespectiveContactCounts.put(accountObject.Name, accountObject.Contacts.size()); | |
} | |
system.debug('accountObject map is: ' + accountNamesAndRespectiveContactCounts); | |
// Too many DML's issue: | |
List<Account> accountList = new List<Account>(); | |
Account accountObject; | |
for (Integer i = 0; i < 150; i++) | |
{ | |
accountObject = new Account(); | |
accountObject.Name = 'Test ' + i; | |
accountList.add(accountObject); | |
} | |
insert accountList; | |
Contact contactObject; | |
for (Account accountIterator : [SELECT Id, Name FROM Account]) | |
{ | |
contactObject = new Contact(); | |
contactObject.AccountId = accountIterator.Id; | |
contactObject.LastName = 'Surname ' + accountIterator.Name; | |
insert contactObject; | |
} | |
// Too many DML's fix: | |
List<Contact> contactList = new List<Contact>(); | |
Contact contactObject; | |
for (Account accountIterator : [SELECT Id, Name FROM Account LIMIT 150]) | |
{ | |
contactObject = new Contact(); | |
contactObject.AccountId = accountIterator.Id; | |
contactObject.LastName = 'Surname ' + accountIterator.Name; | |
contactList.add(contactObject); | |
} | |
if (contactList.size() > 0) | |
{ | |
insert contactList; | |
} | |
system.debug('Size of contact list is: ' + contactList.size()); | |
system.debug('Count of DML statements: ' + Integer.valueOf(Limits.getDmlStatements())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment