非同期 Apex の例
トランザクションセキュリティポリシーを実行する場合、非同期 Apex プロセスを使用して、時間のかかる操作 (外部受信者への通知メールの送信など) をオフロードします。
| 使用可能なインターフェース: Salesforce Classic および Lightning Experience |
| 使用可能なエディション: Enterprise Edition、Unlimited Edition、および Developer Edition Salesforce Shield または Salesforce Event Monitoring アドオンサブスクリプションが必要です。 |
この例は 2 つの部分で構成されます。まず、execute メソッド内のイベントを使用してコールアウトまたは DML 操作を呼び出す非同期 Apex クラスを作成します。次に、トランザクションセキュリティポリシーを作成し、Apex クラスを変更して TxnSecurity.EventCondition と TxnSecurity.AsyncCondition を実装します。
TxnSecurity.AsyncCondition は、トランザクションセキュリティポリシーをトリガーすると、非同期 Apex プロセスをキューに追加します。
非同期 Apex クラスの作成
このセクションでは、SObject を取る非同期 Apex クラスを作成します。次の例では、ApiEvent を使用します。次に、コールアウトまたは DML 操作を呼び出します。
1public class SimpleAsynchronousApex implements Queueable {
2 private ApiEvent apiEvent;
3
4 public SimpleAsynchronousApex(ApiEvent apiEvent) {
5 this.apiEvent = apiEvent;
6 }
7
8 public void execute(QueueableContext context) {
9 // Perform your callout to external validation service
10 // or a DML operation
11 }
12}ポリシーの作成
このセクションでは、トランザクションセキュリティポリシーを作成して、ポリシーに関連付けられた Apex クラスを変更します。次に、SimpleAsynchronousApex オブジェクトを作成して ApiEvent を渡し、ジョブをキューに追加します。
1global class SimpleApiEventCondition implements TxnSecurity.EventCondition, TxnSecurity.AsyncCondition {
2 public boolean evaluate(SObject event) {
3 // Cast SObject to an ApiEvent object
4 ApiEvent apiEvent = (ApiEvent) event;
5 SimpleAsynchronousApex simpleAsynchronousApex = new SimpleAsynchronousApex(apiEvent);
6 System.enqueueJob(simpleAsynchronousApex);
7 return false;
8 // In a typical implementation may return true if it triggers an action
9 }
10}