Recursion in Apex has a bit of a reputation. Many Salesforce developers first hear the term when a trigger unexpectedly calls itself over and over, leading to the dreaded “Maximum trigger depth exceeded” error. It’s easy to assume recursion = bad, especially if you don’t come from a computer science background. In reality, recursion is a fundamental programming concept, a powerful tool when used intentionally, but a source of tricky bugs when it happens unintentionally. This post will simplify recursion in the context of Apex, exploring both the pitfalls of unintended recursive triggers and the value of deliberate recursion for solving certain problems. We’ll look at code examples of each, discuss how recursion interacts with Salesforce governor limits and performance, and cover best practices (like static flags and trigger patterns) to prevent unwanted loops. By the end, you’ll know when to fear recursion, when to embrace it, and how to keep your triggers from going rogue.
Continue readingPosts Tagged → Apex
Salesforce Record Locking and Concurrency – Salesforce Things You should Know
If you build on Salesforce long enough, you will hit a lock. This post explains how Salesforce Record Locking and Concurrency work, why you see UNABLE_TO_LOCK_ROW, and how to write code that behaves correctly under load.
Salesforce Query Plan – Salesforce things you should know
This is Part 2 of Salesforce things you should know. In Part 1 we covered Selective Queries and Indexes. Now let’s turn that theory into practice with the Query Plan. If you want predictable SOQL performance, this is the tool you open before you deploy your code.
Continue readingSelective Queries and Indexes – Salesforce things you should know
I am starting a new series on Salesforce things you should know. Not sure how long I can do this but surely try to keep up. To start with lets jump into one of the salesforce’s most overlooked topics when it comes to Queries – Salesforce Selective Queries and Indexes.
Continue readingChatGPT Multi Turn Chat using Apex
Before diving into ChatGPT multi turn chat capabilities, let’s review what we’ve covered so far. In a previous post, we talked about ChatGPT Salesforce integration using Apex, as well as Google Gemini Salesforce integration, also using Apex. These integrations involved making API calls that allowed for single exchanges with GPT models—meaning we would send one… Continue reading