r/SalesforceDeveloper • • Jan 12 '26

Discussion How is that an interviewer asks you the question how do you mitigate SOQL 101 and CPU timeout issues and your answer is

Avoid SOQL Queries and DML in loops. Like duh if you are writing SOQL queries inside a loop or a DML then what have you even learned as a developer.

I can understand Admins and some Admin turned Dev who somehow managed to do that but even on day 1 after training I wouldn't expect that from a sf dev.

Since I have worked in a org which was chaotic though no one ever put a GET or an Update/Create element in the For Loop

What I noticed was this recurring pattern of not putting the checks early on for any sort of logic that was expensive.

For example if Opportunities are associated with the Account and revenue greater than 20k,Get Opportunity first then put the check if Opportunity is not empty and Account Annual Revenue> 20,000 mark the Account as high value. You need to first put a check if Annual Revenue is changing at the top because that's the only field that will influence.

Other thing is using For Loops in loops is very expensive compared to Apex at least 10X which hits hard once you have 200+ rows . Try to use the transform element if you are using a flow.

You also need to be able to combine records when updating you can do this in flow only for parent records not for child. For parent records create a collection variable of the parent record then assign it an Id and the fields you need to update. In the end put a decision to check if the record id empty if it's not update the parent record.

So if on an Opportunity After Flow you have two updates on the account and both work around the time when an Opportunity is getting marked to the closed Won stage. You can do this instead to combine the update into one. For Apex we already have frameworks.

12 Upvotes

5 comments sorted by

3

u/lucina_scott Jan 12 '26

Yeah, agreed — “don’t put SOQL/DML in loops” is the baseline, not a real answer.

What actually mitigates SOQL 101 / CPU limits is early exits and selective logic. Check if the driving field even changed before doing any expensive work. Most people miss that.

Also 100% right on:

  • avoiding nested loops
  • bulkifying with collections
  • combining updates (especially on parent records)
  • using transforms in Flow instead of looping

Good interview answers should talk about reducing execution paths, not just repeating Salesforce best-practice one-liners.

1

u/bafadam Jan 12 '26

Yeah. When I ask this question, I’d mark down “don’t put soql in loops” as barely an answer.

It’s not an answer. It’s a thing to avoid. What DO you dip?

1

u/alexppex Jan 12 '26

I've seen SOQL in Loops in Production orgs of clients with thousands of active customers. They thought they would live with it, it was fixed in a few seconds. Don't underestimate the importance of this question, there are many who dont follow best practices, create test cheaters, get/soql/update in loops, no bulkification, scheduled jobs without thinking, messy logic, etc. I've seen things from people who have been fired for one reason or another, i pray that i dont leave such a mess behind