Preface: this post is part of the Bulkify Your Code series.
Chapter 5 Questions:
1. True or false. There is always a way around any Governor Limit.
2. True or false. Code will not be able to by deployed if it hits Governor Limits.
3. True or false. Without any changes to code, it’s possible for a trigger to hit Governor Limits on one run, and pass Governor Limits on another.
4. An identical search term has been added to a Map for a second time – this time with a different search result. What happens when the Map is searched using this search term?
(a) An error occurs when adding the second result
(b) The first search result is returned
(c) The second search result is returned
(d) Both search results are returned in a search
5. Different search terms are added to a Map – both with the same search result. What happens when the Map is searched?
(a) An error occurs when adding the second result
(b) When searching the first term, an error occurs
(c) When searching the second term, an error occurs
(d) The same search result is returned for both search terms
6. True or false. It’s OK to have SOQL inside a loop if you know 100% you’ll only have one record in the loop.
7. When combining SOQL queries, why is it better to query against a Set of potential values instead of a List?
Chapter 5 Practice Trigger
Write a trigger that populates an “Owner’s Manager” lookup field on opportunities based on the opp owner’s (user’s) standard ManagerId field. Don’t forget the test class!
Hi David,
Can you please share the link to the Answers for Chapter-5 Quiz?
Hi David,
Firstly thank you so much for all the work you to do create these learning resources, they are incredible!
I am working on the practice trigger for Chapter 5, and find myself wondering if/why maps are necessary to create this trigger effectively. Is it not possible to simply query all of the Opportunities in the trigger, and at this point query the ManagerId, and then iterate over all Opportunities returned in the query and populate the Lookup field this way:
………….
trigger AddManagerLookup on Opportunity (after insert) {
List allOpps = [SELECT Id,
Owner.ManagerId
FROM Opportunity
WHERE Id
IN :trigger.new];
List toUpdate = new List();
for (Opportunity o : allOpps){
if (o.Owner.ManagerId != null){
o.Owner_s_Manager__c = o.Owner.ManagerId;
toUpdate.add(o);
}
}
update toUpdate;
}
……..
I’m sure I’m missing something here, but not sure what :)
Hi All, Test coverage at 91%. Trying to get 100. The one line highligted in red:
oppyOwnerMap.put(u.id,u);
Complete Test code below. Any ideas? Thanks!
@isTest
public class TestAutoPopOwnersManager {
@isTest static void testOppy (){
user uManager = new user(LastName=’smith’,
Alias=’sm’,
Email=’m@m.com’,
Username=’rrrrrr@eeeee.com’,
LocaleSidKey = ‘en_US’,
TimeZoneSidKey = ‘GMT’,
LanguageLocaleKey = ‘en_US’,
EmailEncodingKey = ‘UTF-8′,
ProfileId=’00ei0000001DlHR’);
insert uManager;
user u1 = new user(LastName=’smitty’,
Alias=’smt’,
Email=’sm@sm.com’,
Username=’qwqwqwt2@hghfhfhf.com’,
LocaleSidKey = ‘en_US’,
TimeZoneSidKey = ‘GMT’,
LanguageLocaleKey = ‘en_US’,
EmailEncodingKey = ‘UTF-8′,
ManagerId =uManager.Id,
ProfileId=’00ei0000001DlHR’);
insert u1;
System.runAs(u1){
opportunity o = new opportunity(Name=’new oppy test’,
StageName=’Closed Won’,
CloseDate = date.today(),
OwnerId=u1.Id);
insert o;
}
List newOpps = [SELECT id, Owner_s_Manager__c
FROM Opportunity];
for(Opportunity opps: newOpps){
system.assertEquals(uManager.Id, opps.Owner_s_Manager__c);
}
}
}
Show all the code please to help debug!
hi david,
is ownerId of ooportunity is different from managerId ?
what is managerId ?
i think i got it ,
opportunity owner could be manager like sales manager,SFDC 99 etc or may b manager himself.
managerId is the owner of the orgntzn. like David liu.
Right on!