Category Archives : Apex

How to Deserialize a JSON String in Apex?

On June 9, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on How to Deserialize a JSON String in Apex?

Question: How can I deserialize the following JSON string into Apex? I’ve tried using the following code to deserialize it: The code works, but I’m looking for a more efficient and simpler way to handle this. Any suggestions? Answer: A more efficient and structured approach would be to generate Apex…

How Do I Work with JSON in Apex?

On June 6, 2025, Posted by , In Apex,Salesforce, By ,,, , With Comments Off on How Do I Work with JSON in Apex?

Question This is a canonical question and answer developed by the community to help address common questions. If your question was closed as a duplicate or you were directed here, please review the resources provided and use them to refine your specific inquiry. To explore more canonical Q&A, visit the…

How to use Tooling API in Apex?

On May 29, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on How to use Tooling API in Apex?

Question: How can I use the Tooling API in Salesforce to create an Apex class programmatically within an Apex controller? I am particularly interested in seeing an example that demonstrates how to achieve this. I have already generated MetadataService from WSDL but am open to other approaches, including REST-based methods….

Why Is DML Not Allowed in Constructors?

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

On May 24, 2025, Posted by , In Apex, With Comments Off on 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…

Does a Permission Set in @TestSetup get removed after Test.stopTest()?

On May 20, 2025, Posted by , In Apex,Lightning web components, By ,, , With Comments Off on Does a Permission Set in @TestSetup get removed after Test.stopTest()?

Question If we create a Permission Set inside a @TestSetup method in an Apex test class, will it be automatically removed after Test.stopTest(), similar to how standard or custom object records are deleted at the end of a test execution? For example, consider the following code: After Test.stopTest(), will this…

How to Make a PATCH HTTP Callout in Apex?

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

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…