---
schema_version: "1"
id: 808bd40a2bd3f4d5a9712e8ddebcf967bd259097ac390fe4232f7c64d8460f55
title: Asynchronous Apex Example
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/et_security_policy_apex_dml_example.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.et_security_policy_apex_dml_example.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 8ea56a3cc95050faed5e862d0eaff071de46b4a0ecb1e659383a9a9192604740
description: 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.
products:
  - Salesforce Platform
  - Cross Cloud
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition">
      <colgroup>
        <col>
      </colgroup>
      <tbody>
        <tr class="">
          <td class="slds-cell-wrap" style="vertical-align:top;">Available in both Salesforce Classic (not available in all orgs) and Lightning
            Experience.</td>

        </tr>
        <tr class="">
          <td class="slds-cell-wrap" style="vertical-align:top;">
            <p>Available in: <strong>Enterprise</strong>, <strong>Unlimited</strong>, and <strong>Developer</strong>
              Editions</p>
            <p style="margin-bottom:0;">Requires Salesforce Shield or Salesforce Event Monitoring add-on
              subscriptions.</p>
          </td>

        </tr>
      </tbody>
    </table>

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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-setup-264-0-0-production-enus/6f21df35-9ea0-4f53-ac94-45c3b5dfd5bc/images/icon_note.png)

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.

```java
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.

```java
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
    }
}
```

#### See Also

- [_Apex Developer Guide_: Queueable Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm)
- [_Apex Reference Guide_: Apex Implementation Examples](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_section.htm#apex_dml_section)
- [_Apex Developer Guide_: Asynchronous Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/api/apex_async_overview.htm#!)
- [_Apex Developer Guide_: Invoking Callouts Using Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts.htm)