本篇参考:

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等等。篇中有错误的地方欢迎指出,有不懂欢迎留言。

Salesforce LWC学习(十四) Continuation进行异步callout获取数据的更多相关文章

  1. Salesforce LWC学习(十五) Async 以及 Picklist 公用方法的实现

    本篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type) https://developer.salesfo ...

  2. Salesforce LWC学习(十六) Validity 在form中的使用浅谈

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/lightning-input/documentation h ...

  3. Salesforce LWC学习(五) LDS & Wire Service 实现和后台数据交互 & meta xml配置

    之前的几节都是基于前台变量进行相关的操作和学习,我们在项目中不可避免的需要获取数据以及进行DML操作.之前的内容中也有提到wire注解,今天就详细的介绍一下对数据进行查询以及DML操作以及Wire S ...

  4. Salesforce LWC学习(十) 前端处理之 list 处理

    本篇参看:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array list是我们经 ...

  5. Salesforce LWC学习(十八) datatable展示 image

    本篇参看: https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentati ...

  6. Salesforce LWC学习(十九) 针对 lightning-input-field的label值重写

    本篇参考: https://salesforcediaries.com/2020/02/24/how-to-override-lightning-input-field-label-in-lightn ...

  7. Salesforce LWC学习(三十) lwc superbadge项目实现

    本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...

  8. Salesforce LWC学习(四十) dynamic interaction 浅入浅出

    本篇参考: Configure a Component for Dynamic Interactions in the Lightning App Builder - Salesforce Light ...

  9. Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...

随机推荐

  1. wxpython(2)--按钮,位图按钮,滑动块,微调控制器

    本文介绍按钮,位图按钮,滑动块,微调控制器**.. 按钮 基本按钮 创建一个按钮,绑定点击事件,点击后修改Label 123456789101112131415161718 import wx cla ...

  2. 使用Handler的步骤

    使用Handler的大致流程: 1.首先创建一个Handler对象,可以直接使用Handler无参构造函数创建Handler对象,也可以继承Hander类,重写HandleMessage方法来创建Ha ...

  3. C++走向远洋——47(第十二周、运算符重载基础程序、阅读)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  4. Python左手画条龙右手画个彩虹

    左手画龙右手画彩虹听说很火,Python也可以画出很美的彩虹,准确的说像彩虹棒棒糖:) 效果如下图: # -*- coding: utf-8 -*- # @Time : 2019/12/16 23:2 ...

  5. 微信小程序状态管理工具 JStore

    微信小程序状态管理工具 JStore 闲着没事做,就想着给微信小程序写一个状态管理工具,名叫 JStore,这个状态管理工具是仿照 vuex 的几个方法来写的,所以有 vuex 的基础同学很容易理解. ...

  6. iOS下的 Fixed BUG

    input 光标位置乱窜 固定式浮层内的输入框光标会发生偏移.即 fixed 定位的容器中输入框光标的位置显示不正确,没有正常地显示在输入框中,而是偏移到了输入框外面 可触发条件 页面body出现滚动 ...

  7. 通过HTML及CSS模拟报纸排版总结

    任务目的 深入掌握CSS中的字体.背景.颜色等属性的设置 进一步练习CSS布局 任务描述 参考 PDS设计稿(点击下载),实现页面开发,要求实现效果与 样例(点击查看) 基本一致 页面中的各字体大小, ...

  8. Python - 字符串格式化详解(%、format)

    Python在字符串格式化的两种方式 % format %,关于整数的输出 %o:oct 八进制%d:dec 十进制%x:hex 十六进制 print("整数:%d,%d,%d" ...

  9. 进阶之路 | 奇妙的Thread之旅

    前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 需要已经具备的知识: Thread的基本概念及使用 AsyncTask的基本概念及使用 学习清单: 线程概述 ...

  10. 进程,线程,Event Loop(事件循环),Web Worker

    线程,是程序执行流的最小单位.线程可与同属一个进程的其他线程共享所拥有的全部资源,同一进程中的多个线程之间可以并发执行.线程有就绪,阻塞,运行三种基本状态. 阮一峰大神针对进程和线程的类比,很是形象: ...