Posts

Showing posts with the label Apex Triggers

System.CalloutException: Callout from triggers are currently not supported

The error message "System.CalloutException: Callout from triggers is currently not supported" indicates that a trigger in your Salesforce org is attempting to make a callout to an external system, which is not allowed. Salesforce does not allow callouts to be made directly from triggers. This restriction ensures the integrity and consistency of data in the Salesforce database. If handled improperly, callouts can result in long-running transactions, leading to performance issues or data inconsistencies. For example, when a new Account record is inserted and this trigger fires, you encounter the "System.CalloutException: Callout from triggers are currently not supported" error. trigger AccountTrigger on Account (after insert) {     for (Account acc : Trigger.new) {         // Make a callout to an external API         HttpRequest req = new HttpRequest();         req.setEndpoint('https://api.example.com');    ...

Apex Triggers in Salesforce

Image
In Salesforce, a trigger is a piece of Apex code that executes before or after specific data manipulation events occur, such as inserting, updating, deleting, or undeleting records. Triggers enable you to perform custom actions or validations when these events take place on Salesforce records. Refer to the below links for more details - Get Started with Apex Triggers Apex Triggers Define Apex Triggers Here are some key points about triggers in Salesforce: Execution Context: Triggers execute in response to specific database events and run in the Salesforce environment. Supported Events : Triggers can be defined to execute before or after specific events, including insert, update, delete, undelete, and upsert. Object-Specific: Triggers are associated with specific Salesforce objects, such as Accounts, Contacts, Opportunities, etc. Each trigger operates on records of its associated object type. Apex Language: Triggers are written in Apex, Salesforce's proprietary programming langua...

How to Avoid recursive trigger in salesforce ?

Avoiding recursive triggers in Salesforce is crucial to prevent infinite loops and excessive consumption of system resources. Here are several strategies to achieve this: Use a Static Variable or Flag : Utilize a static Boolean variable or a static set to keep track of whether the trigger logic has already been executed. This prevents the trigger from re-executing when it's not necessary. public class TriggerHandler {     private static Boolean isFirstRun = true;     public static void onBeforeInsert(List<MyObject__c> newList) {         if (isFirstRun) {             // Trigger logic             isFirstRun = false;         }     } } Use a Handler Class : Implement a separate handler class to encapsulate trigger logic. This class can have static variables to manage recursion and can be called from the trigger. public class TriggerHandler {     p...