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:
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.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);
}
1 respuesta