You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Salesforce LWC使其仅对状态为NEW的新建Case生效而非现有Case

LWC组件修改方案

你需要对组件的配置文件、JS逻辑两个部分做调整,HTML代码无需修改即可实现仅状态为NEW的Case打开时触发提示的需求。

1. 修改组件xml配置文件

首先调整配置限定组件仅可在Case记录页使用,同时自动接收当前打开的Case记录ID,修改后代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>CaseType Levels Info Component</masterLabel>
    <description>CaseType Levels Info Component.</description>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordPage">
            <objects>
                <object>Case</object>
            </objects>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

2. 修改JS逻辑增加状态判断

导入记录查询能力和Case状态字段,查询到当前Case的状态后仅当为NEW时触发提示,修改后代码如下:

import { LightningElement, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from 'lightning/uiRecordApi';
// 导入Case对象的Status标准字段
import CASE_STATUS from '@salesforce/schema/Case.Status';

export default class CaseTypeInformation extends LightningElement {
    // 接收当前页面的记录ID和对象API名
    @api recordId;
    @api objectApiName;

    // wire适配器查询当前Case记录的Status字段
    @wire(getRecord, { recordId: '$recordId', fields: [CASE_STATUS] })
    handleCaseRecord({ data, error }) {
        // 仅当查询到记录数据、且对象为Case时执行逻辑
        if (data && this.objectApiName === 'Case') {
            const currentStatus = data.fields.Status.value;
            // 请根据你Org中NEW状态对应的API值调整判断条件,可能为'New'或'NEW'
            if (currentStatus === 'NEW') {
                const toast = new ShowToastEvent({
                    title: 'Case Type Level 1, level 2 and level 3 fields ',
                    message: 'must be selected before saving'
                });
                this.dispatchEvent(toast);
            }
        }
    }
}

3. 注意事项

  • 请先确认你Salesforce环境中Case对象Status字段下「NEW」选项对应的API值,和JS判断的字符串保持一致即可正常触发。
  • 组件部署完成后,需要进入闪电应用构建器打开Case记录页,确认该组件已添加到页面中即可生效。

内容的提问来源于stack exchange,提问作者Carolyn Cordeiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 13:45:04