Skip to main content
Hi All,

I have a scenerio, where an Event Speaker need to be inserted with a Valid Account Status. Here Before Insert of Event Speaker, I need to perform API Callout and get the Status, based on this response Status, I need to restrict the User from creating the Event Spaker.

Can this be possible, please help.

I have tried this, but when we use addError in trigger OR Validaiton rule on Event Speaker, the API Callout is not processed.
1 respuesta
  1. 30 may 2023, 14:18
    Hello Maniraja ,

    Performing an API callout and using the response to restrict the creation of an Event Speaker can be achieved using an Apex trigger. However, it's important to note that making a synchronous API callout in a trigger can cause performance issues and potentially violate Salesforce's governor limits. To overcome this limitation, you can use the @future annotation to perform the API callout asynchronously. Here's an example of how you can implement this:

    Create a new Apex class to handle the API callout:

    public class AccountStatusChecker {

        @future(callout=true)

        public static void checkAccountStatus(Set<Id> accountIds) {

            // Make the API callout and get the account statuses

            

            // Assuming you have the API logic to get the account statuses and store them in a Map<Id, String> where the key is the Account Id and the value is the Account status

            

            // Perform the necessary validation and restrict the creation of Event Speakers

            // You can use addError to prevent the creation of Event Speakers in the trigger

        }

    }

    Create an Apex trigger on the Event Speaker object:

    trigger EventSpeakerTrigger on Event_Speaker__c (before insert) {

        Set<Id> accountIds = new Set<Id>();

        

        for (Event_Speaker__c speaker : Trigger.new) {

            accountIds.add(speaker.Account__c);

        }

        

        AccountStatusChecker.checkAccountStatus(accountIds);

    }

    In this example, the trigger collects the Account Ids from the Event Speakers being inserted and passes them to the AccountStatusChecker class using the @future method. The checkAccountStatus method in the AccountStatusChecker class performs the API callout asynchronously, retrieves the account statuses, and performs the necessary validation to restrict the creation of Event Speakers based on the account statuses.

    By using the @future annotation, the API callout will be executed asynchronously, allowing the trigger to continue processing without waiting for the response from the API callout. This ensures that the trigger is not blocked and the API callout is processed.

    Hope this helps !

    Thank you.