PDII · Question #412
trigger AssignOwnerByRegion on Account ( before insert, before update ) { List<Account> accountList = new List<Account>(); for( Account anAccount : trigger.new ) { Region__c theRegion = [ SELECT Id…
The correct answer is B. Move the Region__c query to outside the loop. C. Remove the last line updating accountList as it is not needed. See the full explanation below for the reasoning.
Question
trigger AssignOwnerByRegion on Account ( before insert, before update ) { List<Account> accountList = new List<Account>(); for( Account anAccount : trigger.new ) { Region__c theRegion = [ SELECT Id, Name, Region_Manager__c FROM Region__c - WHERE Name = :anAccount.Region_Name__c ]; anAccount.OwnerId = theRegion.Region_Manager__c; accountList.add( anAccount ); } update accountList; } Consider the above trigger intended to assign the Account to the manager of the Account's region. Which two changes should a developer make in this trigger to adhere to best practices? (Choose two.)
Options
- AUse a Map to cache the results of the Region__c query by Id.
- BMove the Region__c query to outside the loop.
- CRemove the last line updating accountList as it is not needed.
- DUse a Map accountMap instead of List accountList.
How the community answered
(17 responses)- A18% (3)
- B76% (13)
- D6% (1)
Community Discussion
No community discussion yet for this question.