本篇参考:

https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_subscribe_lc.htm

https://developer.salesforce.com/docs/component-library/bundle/lightning-emp-api/documentation

salesforce零基础学习(九十六)Platform Event浅谈

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

背景: 我们在记录的详情页面,除标准的layout以外,实际工作中也会添加各种各样的component来满足实际业务的需要,有一些是标准组件,有一些是自定义组件。自定义组件间的修改和交互很好弄,我们可以通过 notifyRecordUpdateAvailable搞定(Lighting Data Service)LDS的缓存问题,通过 refreshApex搞定 call apex方法的问题,通过 dispatchEvent / handler方式来搞定父子组件通信的事情,通过pub / sub事件模型来搞定跨组件通讯问题,通过 lightning message service或者 dynamic interaction也可以搞定跨组件通讯问题。如上的内容都是自定义组件之间或者自定义组件的行为渲染到标准组件。那我们如何针对标准组件的更新作用到自定义页面,然后自定义页面捕捉到这些事件操作呢? 本篇提供两种思路。

需求: 当用户在Account详情页面更新数据时,不管使用 quick action的Edit还是 Inline Edit,当Account的Name包含Test的字样,显示一个toast信息。

一. 基于Lightning Data Service(LDS)

这个demo可以基于getRecord的这个wire adapter来实现。因为 getRecord以及标准UI都共用同一个version的数据,所以当标准UI因为用户修改以后,我们通过 getRecord也同样可以自动收到最新的version的数据,所以我们可以基于 getRecord的这个wire adapter来实现。

import { LightningElement, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from "@salesforce/schema/Account.Name";
export default class toastDemo extends LightningElement { @api recordId; @wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD]})
// eslint-disable-next-line no-unused-vars
wiredAccount(data, error) {
if (data.data && data.data.fields && data.data.fields.Name) {
if(data.data.fields.Name.value.includes('test')) {
this.showToast();
}
} else if(error) {
console.log('error');
}
} showToast() {
const evt = new ShowToastEvent({
message: 'Name contains test',
variant: 'info',
});
this.dispatchEvent(evt);
}
}

效果演示:将这个组件放在 lightning record page中

二. 基于Platform Event 订阅实现

如果对Platform Event不了解的,欢迎查看以前的博客内容。思路为当Account Name变动以后,发布一个Account的Platform Event,lwc端用来订阅这个Platform Event,对订阅的结果进行解析,如果满足了预期,则进行逻辑处理。

1. 创建Platform Event的表以及字段。

2. 通过Flow或者Trigger,Account Name包含test情况下,发布Platform Event.

3. lwc进行订阅:这里看一下加粗的两行,messageCallback函数看上去有自己的上下文,如果不传递进去,获取的recordId则为 undefined, context也相同。

import { LightningElement, api, wire } from "lwc";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from "lightning/empApi";
export default class toastDemo extends LightningElement {
channelName = "/event/Account_Change__e"; @api recordId; get changedRecordId() {
return this.targetedRecordId;
} subscription = {}; // Initializes the component
connectedCallback() {
// Register error listener
this.registerErrorListener();
const currentRecordId = this.recordId;
const context = this
;
const messageCallback = function (response) {
// Response contains the payload of the new message received
let event = response.data.payload; if (event.Account_Id__c == currentRecordId) {
const evt = new ShowToastEvent({
message: "Name contains test",
variant: "info"
});
context.dispatchEvent(evt);
}
}; // Invoke subscribe method of empApi. Pass reference to messageCallback
subscribe(this.channelName, -1, messageCallback).then((response) => {
// Response contains the subscription information on subscribe call
console.log("Subscription request sent to: ", JSON.stringify(response.channel));
this.subscription = response;
});
} disconnectedCallback() {
unsubscribe(this.subscription, (unsubResp) => {
console.log("unsubscribe() response: ", JSON.stringify(unsubResp));
});
} registerErrorListener() {
// Invoke onError empApi method
onError((error) => {
console.log("Received error from server: ", JSON.stringify(error));
// Error contains the server-side error
});
}
}

效果:

总结:本篇主要介绍的是标准页面编辑数据情况下自定义的lwc页面如何进行捕捉然后做一些逻辑,其中LDS方式固然好用,但是没有那么灵活,如果需求简单,推荐使用LDS方式,否则可以考虑订阅Platform Event来实现。篇中有错误地方欢迎指出,有不懂欢迎留言。

Salesforce LWC学习(四十七) 标准页面更新以后自定义页面如何捕捉?的更多相关文章

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

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

  2. Salesforce LWC学习(四十二) getRecordNotifyChange已弃用

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get ...

  3. Salesforce LWC学习(四) 父子component交互 / component声明周期管理 / 事件处理

    我们在上篇介绍了 @track / @api的区别.在父子 component中,针对api类型的变量,如果声明以后就只允许在parent修改,son component修改便会导致报错. sonIt ...

  4. Salesforce LWC学习(二十七) File Upload

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/lightning-file-upload/documenta ...

  5. Salesforce LWC学习(三十七) Promise解决progress-indicator的小问题

    本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-progress-indicator/exa ...

  6. Salesforce LWC学习(四十) datatable的dynamic action的小坑浅谈

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

  7. Salesforce LWC学习(四十一) If:true 即将弃用?

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_dir ...

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

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

  9. Salesforce LWC学习(十七) 前端知识之 onclick & onblur & onmousedown

    在Salesforce LWC学习(八) Look Up组件实现篇中,我们实现了公用的lookup组件,使用的过程中,会发现当我们输入内容以后,搜索出来的列表便无法被清空. 针对此种情况我们打算优化一 ...

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

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

随机推荐

  1. xv6 中的进程切换:MIT6.s081/6.828 lectrue11:Scheduling 以及 Lab6 Thread 心得

    絮絮叨 这两节主要介绍 xv6 中的线程切换,首先预警说明,这节课程的容量和第 5/6 节:进程的用户态到内核态的切换一样,细节多到爆炸,连我自己复习时都有点懵,看来以后不能偷懒了,学完课程之后要马上 ...

  2. 运行解压版tomcat中的startup.bat一闪而退的解决办法

    Tomcat的startup.bat,它调用了catalina.bat,而catalina.bat则调用了setclasspath.bat,只要在setclasspath.bat的开头声明环境变量(红 ...

  3. 搭建企业知识库:基于 Wiki.js 的实践指南

    一.简介 在当今知识经济时代,企业知识库的建设变得越来越重要.它不仅有助于企业知识的沉淀和共享,还能提升员工的工作效率,促进企业的创新发展.企业知识库是企业中形成结构化文档,共享知识的集群,可以促进企 ...

  4. SpingCloud:Gateway+Nginx+Stomp+Minio构建聊天室并进行文件传输

    注:本人使用阿里云服务器(安装mino)+本地虚拟机(安装nginx)进行,理论上完全在本地进行也可以. 1.前期准备: 1.将本地虚拟机设置为静态ip且能ping通外网,参考网址:https://w ...

  5. numpy中的nan (无穷小)注意点

  6. skynet的timer似乎有问题

    skynet.timeout 传进去 number 范围内的数值但是会溢出, 调查发现 skynet.timeout 调用的是 c 的方法: c.intcommand("TIMEOUT&qu ...

  7. 2023-09-27:用go语言,在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始, 并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格是 (0

    2023-09-27:用go语言,在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始, 并尝试进行 k 次移动.行和列是 从 0 开始 的,所以左上单元格是 (0 ...

  8. Python正则表达式——常用re正则表达式集合

    文章目录 一.校验数字的表达式 二.校验字符的表达式 三.特殊需求表达式 一.校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^ ...

  9. 关于tiptop gp5.2采购模块,价格变更的随笔

    采购价格变更要看具体环节,你可以把他当作是三张表,采购价格表.收货价格表.入库价格表,这些还好处理,如果已抛砖到财务端生成账款再要求改价格就更复杂,会产生更多张表了,改起来也就更复杂. 用apmt91 ...

  10. CUDA C编程权威指南:2.2-给核函数计时

      本文主要通过例子介绍了如何给核函数计时的思路和实现.实现例子代码参考文献[7],只需要把相应章节对应的CMakeLists.txt文件拷贝到CMake项目根目录下面即可运行. 1.用CPU计时器计 ...