Loading
Set Up and Maintain Your Salesforce Organization
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Asynchronous Apex Example

          Asynchronous Apex Example

          When executing a transaction security policy, use an asynchronous Apex process to offload time-consuming operations, such as sending a notification email to an external recipient.

          Required Editions

          Available in both Salesforce Classic (not available in all orgs) and Lightning Experience.

          Available in: Enterprise, Unlimited, and Developer Editions

          Requires Salesforce Shield or Salesforce Event Monitoring add-on subscriptions.

          This example has two parts. First, you create an asynchronous Apex class that uses an event within the execute method to invoke a callout or a DML operation. Second, you create a transaction security policy and modify the Apex class to implement TxnSecurity.EventCondition and TxnSecurity.AsyncCondition.

          TxnSecurity.AsyncCondition enqueues the asynchronous Apex process when you trigger the transaction security policy.

          Note
          Note Only DML operations and callouts are supported when you use asynchronous Apex with an enhanced transaction security policy.

          Create Asynchronous Apex Class

          In this section, you create an asynchronous Apex class that takes in an SObject. In this example, we use ApiEvent. Then you invoke a callout or a DML operation.

          public class SimpleAsynchronousApex implements Queueable {
              private ApiEvent apiEvent;
          
              public SimpleAsynchronousApex(ApiEvent apiEvent) {
                  this.apiEvent = apiEvent;
              }
          
              public void execute(QueueableContext context) {
                  // Perform your callout to external validation service
                  // or a DML operation
              }
          }

          Create Policy

          In this section, you create the transaction security policy, which modifies the Apex class associated with the policy. Then you create the SimpleAsynchronousApex object, pass in the ApiEvent, and enqueue the job.

          global class SimpleApiEventCondition implements TxnSecurity.EventCondition, TxnSecurity.AsyncCondition {
              public boolean evaluate(SObject event) {
                  // Cast SObject to an ApiEvent object
                  ApiEvent apiEvent = (ApiEvent) event; 
                  SimpleAsynchronousApex simpleAsynchronousApex = new SimpleAsynchronousApex(apiEvent);
                  System.enqueueJob(simpleAsynchronousApex);
                  return false;
                  // In a typical implementation may return true if it triggers an action
              }
          }
           
          Loading
          Salesforce Help | Article