7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SalesforceのLightning Componentから長時間コールアウトを実行する

Posted at

Summer '19リリース(APIバージョン46.0)で、Lightning Component(Aura/LWC)から直接長時間コールアウト(非同期コールアウト)が実行できるようになりました。Salesforceプラットフォームでは、5秒を超えるApexトランザクション数が同時に10までという制限があり、非機能要件を検討する際にしばしば必要となる観点ですが、サーバサイドの処理が必要でかつ外部システムとの連携が多いシナリオにおいては、長時間コールアウトはこの制限を回避する方法の1つとなります。1

Apexメソッドの定義

長時間コールアウトを行うApexのメソッドに対しては、@AuraEnabled(continuation=true)のアノテーションを付与します。Continuationクラスをインスタンス化して、コールバックメソッドを指定し、通常のHttpRequestを渡せば良いです。

@AuraEnabled(continuation=true cacheable=true)
public static Continuation exampleRequest() {
    Continuation con = new Continuation(60); //引数はタイムアウト時間(最大120秒)
    con.continuationMethod = 'continuationCallback';
    
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setEndpoint('callout:Example');
    con.addHttpRequest(req);
    return con;
}

@AuraEnabled(cacheable=true)
public static String continuationCallback(List<String> labels, Object state) {
    HttpRespons res = Continuation.getResponse(labels.get(0));
    return res.getBody();
}

Continuationを返すメソッド内ではDMLが実行できませんので注意してください。コールバックメソッド内では許可されています。また、余談ですが、エンドポイントの設定には指定ログイン情報(Named Credential)を用いると便利です。直接指定する場合はリモートサイト設定を忘れずに行いましょう。

Aura Componentから実行

通常のApex呼び出しと同様です。

({
    handleContinuation : function(component, event, helper) {
        const action = component.get("c.exampleRequest");
        action.setCallback(this, response => {
             const state = response.getState();
             if(state === "SUCCESS"){
                  //成功時の処理
             } else if(state === "ERROR"){
                  //エラー時の処理
             }
        });
        $A.enqueueAction(action);
    }
})

Lightning Web Componentから実行

こちらも通常のApex呼び出しと同様ですが、import時のパスが@salesforce/apexではなく、@salesforce/apexContinuationとなることに注意してください。

import { LightningElement, track } from 'lwc';
import exampleRequest from '@salesforce/apexContinuation/ContinuationExampleController.exampleRequest';

export default class ContinuationExample extends LightningElement {
    @track result;
    @track error;
    //例えばボタン呼び出しの場合
    handleContinuation() {
        exampleRequest()
            .then(result => {
                this.result = result;
            })
            .catch(error => {
                this.error = error;
            });
    }
}

参考

  1. シンプルなコールアウトであればクライアントサイドでFetchやXHRで実装すれば良いですが、ここでは扱いません。
    これまで、Lightning Componentから長時間コールアウトを実行するには、Visualforceページをラップする必要がありましたが、この方法は煩雑かつあまりセキュアではありませんでした。ここでは、Lightning Componentから直接長時間コールアウトを実行する方法をサンプルコードとともに紹介します。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?