Category: Salesforce Apex
-
How to Auto-Populate “To Address” in Send Email?
Question I have a requirement to create a Quick Action button on the standard Case object to send emails. The “To Address” field should always auto-populate with a predefined email address, regardless of previous emails sent from the case. To achieve this, I created an object-specific Quick Action (Send Email) and set the “To Address”…
-
How to Handle Translations in 2GP Package?
Question I am working on a second-generation managed package and need to add translations for some Lightning Web Components (LWCs) into German. The translations are implemented using Custom Labels, stored in CustomLabels.labels-meta.xml, and the German translations are stored in de.translation-meta.xml inside the translations folder. Before creating a new beta version of the package, I added…
-
How to Avoid Excessive Looping in Salesforce Triggers?
Question I am relatively new to Salesforce development and have been exploring different design patterns to write efficient and maintainable code. In my current structure, I have a trigger, a handler class, and a service class that processes records. Each service method iterates over the same set of records for different operations. As the number…
-
How to Block Recursion in Generic Trigger Handlers?
Question Blocking recursion in Apex trigger handlers is a critical concern, especially when your handler is designed to cover all trigger operations and handle SObjects of any type. Recursion occurs when a trigger causes changes that subsequently invoke the same trigger again, resulting in an infinite loop. This is particularly problematic when operations like updates…
-
How to Log Lead Inserts Without Duplicates?
Question I have a requirement to log every attempt to insert a Lead into a custom object called Lead_Log__c. This log needs to be created regardless of whether the Lead insert is successful or fails validation. To achieve this, I initially used a before insert trigger on the Lead object. However, I faced challenges with…
-
How to Return HTTP 200 in Apex REST?
Question I am new to working with webhooks and need to expose an Apex class to an external system to handle a POST operation in Salesforce. Specifically, I am integrating with Authorize.net, which requires the webhook endpoint to return an HTTP 200 status code. Here’s the screenshot of API documentation. Here’s the debug output from…
-
Why is instanceof inconsistent in Apex collections?
Question In Apex, the following behavior seems inconsistent: My Question: Answer The Apex type system, particularly regarding collections like Map, Set, and List, has certain inconsistencies that can cause confusion. Consider the following code snippet: This behavior raises the question: Why does m instanceof Map<String, Object> evaluate to true, but m instanceof Map<Object, Object> evaluates…