Why Is DML Not Allowed in Constructors?
Question: Why is it not allowed to perform DML operations within an Apex class constructor? Is there a specific reason for this restriction? Despite searching extensively, the rationale behind this limitation doesn’t appear to be well-documented. Answer: DML operations are not allowed in constructors because of potential side effects that…
How to Avoid DML Operations from For Loops in Salesforce
Table Of Contents In Salesforce development, optimizing code for performance and scalability is paramount. One common mistake that developers make is performing DML (Data Manipulation Language) operations inside loops. While this may seem like an easy solution, it can result in hitting Salesforce’s governor limits, causing system errors or slow…
How to Make a PATCH HTTP Callout in Apex?
Question: How can I make a PATCH HTTP callout from Apex? I discovered that the HttpRequest class in Apex does not directly support the PATCH method. When attempting to send a PATCH request, I receive a System.CalloutException: Invalid HTTP method: PATCH. This is problematic because I need to call out…
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?
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?
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
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?
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?
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…

