Salesforce LWC學習(十四) Continuation進行非同步callout獲取資料
本篇參考:
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_continuations
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Continuation.htm#apex_class_System_Continuation
我們在專案中經常遇到會和後臺apex進行互動通過SOQL/SOSL去獲取資料展示在前臺。當然,有些場景下資料是儲存在外部系統,需要apex進行callout操作去獲取資料展示前端。lwc針對callout操作可以簡單的分成幾步走,我們這裡以
一. Enable Remote Site
針對外部系統的互動,我們第一步就是要先在salesforce系統中配置Remote Site,才可以訪問,否則會報錯。我們以https://th-apex-http-callout.herokuapp.com/這個trailhead提供的callout URL作為 remote site 的配置,這個URL返回的值為: {"trailhead":"is awesome."}
二. 前後臺構建
我們以前做callout通常通過HttpRequest,然後將設定對應的header, url , body等以後然後Http.sendRequest即可實現外部系統callout互動。在lwc中,我們需要使用 Continuation這個salesforce提供的類進行互動,具體使用和文件可以檢視最上方的連結。我們在lwc和apex互動需要設定 @AuraEnabled=true,這個同樣需要,在這個基礎上,需要設定continuation=true,如果請求資料是固定的,可以也設定cacheable=true從而增加效率,都宣告情況下寫法如下
@AuraEnabled(continuation=true cacheable=true)
除了這裡的小變動,另外的改變就是不用Http.sendRequest方式來構建,而是使用 Continuation方式來構建。Continuation建構函式只有一個引數,用來設定time out時間,以秒為單位。他有幾個引數,continuationMethod用來設定訪問以後對應的回撥函式。timeout用來設定超時時間,最多120秒。state設定用來當callout操作完成並且callback方法執行完成以後的狀態值。我們可以用這個狀態值來確定當前的callout操作是否執行完成。Continuation有三個常用的方法:
addHttpRequest/getRequests()/getResponse()這三個方法的詳情描述自行檢視上方的API文件。
ContinuationDemoController類描述如下:宣告startRequest方法用來callout指定的service URL,然後將response放在callback函式中進行返回。
public with sharing class ContinuationDemoController { // Callout endpoint as a named credential URL // or, as shown here, as the long-running service URL private static final String LONG_RUNNING_SERVICE_URL = 'https://th-apex-http-callout.herokuapp.com/'; // Action method @AuraEnabled(continuation=true cacheable=true) public static Object startRequest() { // Create continuation. Argument is timeout in seconds. Continuation con = new Continuation(40); // Set callback method con.continuationMethod='processResponse'; // Set state con.state='Hello, World!'; // Create callout request HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(LONG_RUNNING_SERVICE_URL); // Add callout request to continuation con.addHttpRequest(req); // Return the continuation return con; } // Callback method @AuraEnabled(cacheable=true) public static Object processResponse(List<String> labels, Object state) { // Get the response by using the unique label HttpResponse response = Continuation.getResponse(labels[0]); // Set the result variable String result = response.getBody(); return result; } }
continuationCmp.html:用來展示從遠端伺服器端的內容
<template> <div> service content: {formattedWireResult} </div> </template>
continuationCmp.js:寫法上和訪問apex class方法沒有任何不同
import { LightningElement,wire } from 'lwc'; import startRequest from '@salesforce/apexContinuation/ContinuationDemoController.startRequest'; export default class ContinuationComponent extends LightningElement { // Using wire service @wire(startRequest) wiredContinuation; get formattedWireResult() { return JSON.stringify(this.wiredContinuation); } }
結果:將遠端伺服器內容轉換成JSON字串
總結:篇中只是簡單介紹了Continuation的介紹,還有很多的細節的操作和限制沒有在本篇中說出,比如Continuation和DML操作前後關係等限制,相關的limitation等等。篇中有錯誤的地方歡迎指出,有不懂歡迎留