Category Archives : Apex

Assembling HTML Letterhead Emails in APEX?

On May 15, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Assembling HTML Letterhead Emails in APEX?

Question: How can I merge HTML from an EmailTemplate and BrandTemplate into a single, fully-assembled HTML document using APEX? I am working on an email service integration project and need to combine the contents of two Salesforce objects: Salesforce does not natively support CDATA manipulation, and my attempts to parse…

How to Unit Test Callouts in Apex?

On May 13, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on How to Unit Test Callouts in Apex?

Question: How do I unit test Apex code that includes a SOAP or REST-based callout or indirectly calls code that makes a callout? I am getting an error saying that callouts are not supported during test execution. Answer: Salesforce enforces test isolation, which means you cannot make REST or SOAP…

How to Fix Apex Test Failures Due to DML Limit?

On May 10, 2025, Posted by , In Apex,Salesforce, By ,, , With Comments Off on How to Fix Apex Test Failures Due to DML Limit?

Question I am trying to deploy an additional field into an existing Aura component bundle in my production org to improve the user interface. However, I keep encountering Apex test failures in the following test classes: The error message I receive is: The stack trace points to these methods: Here…

Efficiently Generate a Set from List

On May 9, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Efficiently Generate a Set from List

Question: I have a SOQL query that returns a List<SObject> structure, and I need to convert this into a Set<Id> to pass to another method. The method’s signature cannot be changed, so I must provide the data in the required format. What is the best way to achieve this conversion?…

How to Test Apex code with Field History Tracking?

On May 6, 2025, Posted by , In Apex,Salesforce Technical Questions, By ,,, , With Comments Off on How to Test Apex code with Field History Tracking?

Question In Salesforce, testing Apex code that relies on field history tracking can be challenging because field history records are not automatically created during tests. This issue arises when the code queries history records, such as those for custom objects. Although the code works in production, the tests fail because…

Why is Schema.describeSObjects Slower Than Schema.getGlobalDescribe?

On May 1, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Why is Schema.describeSObjects Slower Than Schema.getGlobalDescribe?

Question I wanted to reconfigure my code to use the newer Schema.describeSObjects(types) method, expecting it to be faster than the older Schema.getGlobalDescribe approach. However, after testing, I found that Schema.describeSObjects is significantly slower. Here’s the code I used for comparison: New DescribeSObjects Method: Old GetGlobalDescribe Method: Over five runs, the…

How to Write Unit Tests for Triggers?

On April 25, 2025, Posted by , In Apex, With Comments Off on 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?

On April 22, 2025, Posted by , In Apex, With Comments Off on 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?

On April 19, 2025, Posted by , In Apex, With Comments Off on 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?

On April 16, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on 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…