首页 > 其他分享 >Salesforce LWC学习(四十七) 标准页面更新以后自定义页面如何捕捉?

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

时间:2023-12-26 11:12:39浏览次数:21  
标签:Account Salesforce 自定义 组件 data Event 页面

本篇参考: 

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, one rror, 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 one rror empApi method
    one rror((error) => {
      console.log("Received error from server: ", JSON.stringify(error));
      // Error contains the server-side error
    });
  }
}

效果:

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

 

标签:Account,Salesforce,自定义,组件,data,Event,页面
From: https://www.cnblogs.com/zero-zyq/p/17927154.html

相关文章

  • 微信页面公众号页面 安全键盘收起后键盘下方页面留白
      微信浏览器打开H5页面和公众号页面,输入密码时调起安全键盘,键盘收起后键盘下方页面留白解决办法: 1、(简单)只有在调起安全键盘(输入密码)的时候会出现这种情况,将input属性改为number,添加一个加密样式就可以了<inputtype="npmber"name="password"placeholder="请输入......
  • 跨页面新手引导 思路
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=......
  • [CSS]border-image-slice宽高不确定时自定义边框
    宽高不确定时自定义边框效果: <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>borde......
  • 自定义指令
    介绍​除了Vue内置的一系列指令(比如v-model或v-show)之外,Vue还允许你注册自定义的指令(CustomDirectives)。我们已经介绍了两种在Vue中重用代码的方式:组件和组合式函数。组件是主要的构建模块,而组合式函数则侧重于有状态的逻辑。另一方面,自定义指令主要是为了重用......
  • 【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
    问题描述当在AppServiceforWindows环境中所列出的TomcatVersion没有所需要的情况下,如何实现自定义Tomcat环境呢? 问题解答第一步: 从官网下载要使用的 tomcat版本,解压到本地目录第二步:修改 conf/server.xml 配置文件将 port 改成 -1:将 Http-connectport 改成 ${po......
  • 【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
    问题描述当在AppServiceforWindows环境中所列出的TomcatVersion没有所需要的情况下,如何实现自定义Tomcat环境呢? 问题解答第一步: 从官网下载要使用的tomcat版本,解压到本地目录 第二步:修改 conf/server.xml 配置文件将 port 改成 -1:将 Http-connect......
  • EasyCVR点击通道后页面分页不显示是什么原因?如何解决?
    有用户反馈,EasyCVR在点击当前通道列表分页后,再点击其他设备时,页面不加载任何通道,如下对比:经过技术人员排查后发现,原因是重新点击设备时,带了之前的分页数据,才导致页面没有数据;查看代码发现,设备编码变更时,没有重置分页参数;于是新增重置分页参数代码,即可解决该问题。......
  • 智能监控平台/视频共享融合系统EasyCVR点击通道后页面分页不显示是什么原因?如何解决?
    TSINGSEE青犀视频监控汇聚平台EasyCVR可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安防视频监控的能力,也具备接入AI智能分析的能力,包括对人、车、......
  • keycloak~对接login-status-iframe页面判断用户状态变更
    上次我们说了,keycloak的login-status-iframe页面的作用,并解决了跨域情况下,iframe与主页面数据传递的方法,这一次,我们主要分析login-status-iframe.html这个文件的源码,然后分析在我们系统中如何与这个页面对接。login-status-iframe.html源码<script>varinit;function......
  • 使用js进行页面跳转
    场景vue项目,vite配置了基本路径前缀/h5在页面内,通过js进行页面跳转问题使用window.location.href='目标地址',结果发现实际跳转地址会变成本地地址前缀+目标地址,e.g. https://loacalhost:9999/h5/www.baidu.com解决同样使用window.location.href,但是不是直接进行目标地址......