How to Write Unit Tests for Triggers?
Question: How can I write unit tests for a Salesforce trigger? What are the best practices for creating these tests? Should I include the test code inline with the trigger itself, or should I create a separate class for testing? Additionally, are there any approaches or utilities that make writing…
SOQL Record Locking Behavior for Update?
Question: I am working on implementing record locking in Apex using the FOR UPDATE clause, but I encountered some confusion regarding its behavior. The official Salesforce documentation states that when a second transaction attempts to lock a record already locked by another transaction, it will throw a QueryException. However, answers…
How to Fix TraceFlag Creation Errors in Apex?
Question: I am attempting to create a TraceFlag from Invocable Apex to debug an Experience Cloud screen flow. The initial issue was a “Bad Request” error (Status Code 400). After some debugging, I learned that the DebugLevelId field is required for TraceFlag creation, and not specifying individual debug level settings…
Generic Exception Handling in Apex: Is It Acceptable?
Question: In Apex, is it ever acceptable or preferable to catch a generic Exception? While I generally discourage this practice—often referred to as a “Pokemon Catch” (“gotta catch ’em all!”)—there are situations where it seems unavoidable. For example, consider the following scenario: My concern is that catching Exception demonstrates a…
Why Does Salesforce CLI Authentication Fail in VS Code?
Question When trying to authenticate a Salesforce org in VS Code, the process successfully redirects to the browser for login, and after entering credentials, the browser confirms “Authentication successfully.” However, in VS Code, the status remains stuck at “in progress,” and if canceled, the following error message appears: Answer This…
How to filter Opportunity Owner ID in a report using Apex?
Question: When building a report programmatically using Apex, how can you set up a filter for the “Owner ID” field on the Opportunity object? Specifically, I tried to use the ReportFilter from the Reports namespace to set the filter for the Owner ID but encountered errors. Here’s the code I…
How Does Test Coverage Work for Enums and Properties in Apex?
Question: I recently noticed some unusual behavior in Apex test coverage and wanted to confirm whether this has always been the case or if something has changed over time. Specifically, I observed that: Consider the following class containing only an enum: Even when this enum is used in test cases,…
Loop with SOQL or List, Which is Better?
Question: When working with Salesforce Apex, is it better to use a SOQL query directly within a for loop, or to query the records into a list first and then iterate through that list? Considerations include performance, memory usage, and specific use cases. Here are two examples of the approaches…
Managing Batch Execution Without DML Limits in Salesforce
Question How to Create Accounts with Multiple Contacts in Salesforce Without Exceeding DML Limits? I am trying to create accounts and associate 1,000 contacts with each account in Salesforce using a batch class. When managing batch execution without DML limits in Salesforce, I can successfully handle the required number of…
Can You Set Lookup Fields with __r and External IDs?
Question Is it possible to set a lookup field in Apex by using the relationship field (__r) and an external ID? For example, can we relate a child record to its parent by specifying the parent object with just an external ID value? If yes, how does it work, and…

