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 101 discussion

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

What is a potential design issue with the following code?
trigger accountTrigger on Account (before update){ Boolean processOpportunity = false; List<opportunity> opptysClosedLost = new List<opportunity>() List<opportunity> IstAllOpp = [select StageName from Opportunity where accountId IN :Trigger.newMap.keySet()]; if(!IstAllOpp.isEmpty()) processOpportunity = true; while(processOpportunity)
{ for(opportunity o : IstAllOpp) if(o.StageName == 'Closed - Lost') opptysClosedLost.add(o); processOpportunity = false; if(!opptysClosedLost.isEmpty()) delete opptysClosedLost;

  • A. SOQL could be avoided by creating a formula field for StageName in Account from the related Opportunity
  • B. The code will result in a System.LimitException : Too many script statements error
  • C. The code will result in a System.DmlException:Entity_is_Deleted error
  • D. The code will result in a System.LimitException: Apex CPU time limit exceeded error
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
bsf_96
3 months, 1 week ago
Answer is D If no opportunity is "Closed-Lost", code will Infinitely loop resulting in Apex CPU time limit error
upvoted 1 times
...
FriedConsole2000
9 months ago
Selected Answer: B
Shouldn't it be "B". Wouldn't this be a infinite loop if no stageName == 'Closed - Lost'?
upvoted 1 times
...
moitam
1 year, 2 months ago
Selected Answer: D
I will just add my 2 cents here... The best option is D. The code will result in a System.LimitException: Apex CPU time limit exceeded error if the trigger is invoked with a large number of records. The other options are not correct because: A. Creating a formula field for StageName in Account will not avoid SOQL, it will just add an extra field to the Account object that may not be accurate or useful. B. The code will not result in a System.LimitException: Too many script statements error because the number of script statements is not dependent on the number of records processed by the trigger. C. The code will not result in a System.DmlException:Entity_is_Deleted error because the records are deleted only once and not queried again after deletion.
upvoted 1 times
...
lorenac2
1 year, 10 months ago
I agree with it being a bad question. I can't tell where one code block begins and ends. A is irrelevant B is related to running too many lines of code, unlikely because a trigger runs in batches of 200 Maybe somehow it is C depending on how it is supposed to be written D is also unlikely unless this ended in an infinite loop
upvoted 1 times
lorenac2
1 year, 10 months ago
No, not C, C is when a different record is added or updated and is associated with a deleted record: https://help.salesforce.com/s/articleView?id=000383970&type=1 So then my guess is that this was supposed to end in an infinite loop; in short, a code block that loops and never meets the exit condition.
upvoted 1 times
...
lorenac2
1 year, 10 months ago
Here is a better format of the code in case someone else wants to compare: trigger accountTrigger on Account (before update){ Boolean processOpportunity = false; List<opportunity> opptysClosedLost = new List<opportunity>(); List<opportunity> IstAllOpp = [select StageName from Opportunity where accountId IN :Trigger.newMap.keySet()]; if(!IstAllOpp.isEmpty()){ processOpportunity = true; while(processOpportunity){ for(opportunity o : IstAllOpp){if(o.StageName == 'Closed - Lost'){ opptysClosedLost.add(o); processOpportunity = false;}}} if(!opptysClosedLost.isEmpty()){delete opptysClosedLost;} The design flaw is that neither loop is necessary, a proper query could have collected the correct records to begin with and then delete them.
upvoted 3 times
...
...
mageshrr
2 years ago
During testing, No Error was Found, in fact, the opportunity record was deleted successfully. Therefore, None of the answers listed are not matching. I am not sure whether the question is correct.
upvoted 1 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 ...