Apex クラスでの継続の処理

Apex クラスで継続を処理するには、Apex Continuation オブジェクトを使用します。

  1. 外部サービスをコールする前に、Salesforce ユーザインターフェース内の認証されたリモートサイトのリストにそのリモートサイトを追加する必要があります。[設定] から、[クイック検索] ボックスに「Remote Site Settings」と入力します。[リモートサイトの設定] を選択して、[新規リモートサイト] をクリックします。次の Apex クラスの継続の例に示すように、LONG_RUNNING_SERVICE_URL に対応するコールアウトの URL を追加します。 コールアウトでエンドポイントとして指定ログイン情報を指定する場合、リモートサイト設定を定義する必要はありません。指定ログイン情報では、1 つの定義にコールアウトエンドポイントの URL と必要な認証パラメータを指定します。コードに、長時間サービスの URL ではなく、指定ログイン情報 URL を指定します。

  2. 長時間のコールアウトを実行するには、Continuation オブジェクトを返す Apex メソッドを定義します。(@AuraEnabled アノテーションの属性についてはまだ気にしないでください。まもなく説明します。)

    1@AuraEnabled(continuation=true cacheable=true)
    2public static Object startRequest() {
    3    // Create continuation. Argument is timeout in seconds.
    4    Continuation con = new Continuation(40);
    5    // more to come here
    6    return con;
    7}
  3. コールアウトの完了後に Apex コールバックメソッドが呼び出されるように Continuation オブジェクトの continuationMethod プロパティで設定します。この例のコールバックメソッドは processResponse です。コールバックメソッドは、同じ Apex クラスに属している必要があります。

    1con.continuationMethod='processResponse';
  4. HttpRequest オブジェクトを Continuation オブジェクトに追加して、コールアウトのエンドポイントを設定します。1 つの Continuation オブジェクトに最大 3 個のコールアウトを含めることができます。各コールアウトには、リモートサイトまたは指定ログイン情報が [設定] で定義されている必要があります。

    1HttpRequest req = new HttpRequest();
    2req.setMethod('GET');
    3req.setEndpoint(LONG_RUNNING_SERVICE_URL);
    4con.addHttpRequest(req);
  5. コールバックメソッドに渡すデータを Continuation オブジェクトの state プロパティで設定します。state プロパティには Object 型があるため、Apex でサポートされているすべてのデータ型を渡すことができます。

    1con.state='Hello, World!';
  6. Apex コールバックでロジックをコーディングします。Continuation オブジェクトに設定されているすべてのコールアウトが完了すると、Apex コールバックメソッドの processResponse が呼び出されます。このコールバックメソッドには 2 つのアクセス可能なパラメータがあります。

    1public static Object processResponse(List<String> labels, Object state)
    1. labels — 継続に含まれる各リクエストのラベルのリスト。ラベルは自動的に作成されます。
    2. stateContinuation オブジェクトの state プロパティで設定した状態。
  7. 継続に含まれる各要求に対する応答。次に例を示します。

    1HttpResponse response = Continuation.getResponse(labels[0]);
  8. 結果をコンポーネントの JavaScript ファイルに返します。

例: 継続を使用した Apex クラス 

上記のすべてのステップを 1 つにまとめた完全な Apex クラスの例を示します。

1public with sharing class SampleContinuationClass {
2    // Callout endpoint as a named credential URL
3    // or, as shown here, as the long-running service URL
4    private static final String LONG_RUNNING_SERVICE_URL =
5        '<insert your callout URL here>';
6
7    // Action method
8    @AuraEnabled(continuation=true cacheable=true)
9    public static Object startRequest() {
10      // Create continuation. Argument is timeout in seconds.
11      Continuation con = new Continuation(40);
12      // Set callback method
13      con.continuationMethod='processResponse';
14      // Set state
15      con.state='Hello, World!';
16      // Create callout request
17      HttpRequest req = new HttpRequest();
18      req.setMethod('GET');
19      req.setEndpoint(LONG_RUNNING_SERVICE_URL);
20      // Add callout request to continuation
21      con.addHttpRequest(req);
22      // Return the continuation
23      return con;
24    }
25
26    // Callback method
27    @AuraEnabled(cacheable=true)
28    public static Object processResponse(List<String> labels, Object state) {
29      // Get the response by using the unique label
30      HttpResponse response = Continuation.getResponse(labels[0]);
31      // Set the result variable
32      String result = response.getBody();
33      return result;
34    }
35}

The Japanese Summer '24 guide is now live

日本語の Summer '24 ガイドが公開されました! 「Component Reference (コンポーネントリファレンス)」は、以前と同様にコンポーネントライブラリにあります。