Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam Certified Platform Developer II All Questions

View all questions & answers for the Certified Platform Developer II exam

Exam Certified Platform Developer II topic 1 question 47 discussion

Actual exam question from Salesforce's Certified Platform Developer II
Question #: 47
Topic #: 1
[All Certified Platform Developer II Questions]

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.)

  • A. Use a Map to cache the results of the Region__c query by Id.
  • B. Move the Region__c query to outside the loop.
  • C. Remove the last line updating accountList as it is not needed.
  • D. Use a Map accountMap instead of List accountList.
Show Suggested Answer Hide Answer
Suggested Answer: BC 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
sajjankrgupta
Highly Voted 3 years, 11 months ago
Using a SOQL query inside for loop is bad practice and since it is before insert/update no need to perform DML. Ans is B and C.
upvoted 34 times
...
AB1993
Highly Voted 3 years, 3 months ago
B & C is the correct answer
upvoted 9 times
...
Jeet89123
Most Recent 5 months, 2 weeks ago
Selected Answer: BC
B and C correct as per best parctices
upvoted 1 times
...
MetaLojiqTee
1 year, 3 months ago
Selected Answer: BC
B) Performing Queries inside a loop is not good practice, C) Changes made to records retrieved from 'Before' trigger context are automatically persisted if the trigger transaction completes successfully
upvoted 1 times
...
ram12313
1 year, 7 months ago
B&C is correct if we're only talking about best practices. But we still need to tidy it up a bit like code below. trigger AssignOwnerByRegion on Account ( before insert, before update ) { Set<String> acctRegionNames = new Set<String>(); for( Account acc : Trigger.new){ acctRegionNames.add(acc.Region_Name__c); } Map<String, String> regionMap = new Map<String, String>(); for( Region__c reg : [SELECT Name, Region_Manager__c FROM Region__c Name IN :acctRegionNames]){ regionMap.put(reg.Name, reg.Region_Manager__c); } for( Account anAccount : trigger.new ){ if(regionMap.containsKey(anAccount.Name)){ anAccount.OwnerId = regionMap.get(anAccount.Name); } } }
upvoted 4 times
...
Genki_Sugisaki
1 year, 8 months ago
Selected Answer: BC
B&C A is OK if Use a Map to cache the results of the Region__c query by Name
upvoted 2 times
...
amilaveer
1 year, 11 months ago
Selected Answer: BC
B & C are correc t
upvoted 1 times
...
sf2022
1 year, 11 months ago
Selected Answer: BC
BC are correct answers
upvoted 1 times
...
Dean5555
2 years, 1 month ago
Selected Answer: BC
B&C it is
upvoted 1 times
...
levian
2 years, 1 month ago
B and C, for sure
upvoted 1 times
...
Pratibhadx
3 years, 1 month ago
B & C obviously
upvoted 6 times
...
amerbearat
3 years, 3 months ago
b and c is the answer as it is a before trigger we don’t need to update the account and soql always outside of the loop
upvoted 6 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...