Search code examples
salesforcesalesforce-lightninglwcsalesforce-communities

How To Make Salesforce LWC work only for New Cases and Not on Existing cases


I have a requirement where LWC component is fired everytime when a case is opened,I want to change LWC component to work only for NEW cases,what changes needs to be done in the LWC to make it work only for specific case types which are in NEW status

Here is JS code

    import { LightningElement } from 'lwc';
    import {ShowToastEvent} from 'lightning/platformShowToastEvent';

     export default class CaseTypeInformation extends LightningElement {


    connectedCallback() {
        var toast = new ShowToastEvent({
                 'title': 'Case Type Level 1, level 2 and level 3 fields ',
                 'message': 'must be selected before saving'
             });
             this.dispatchEvent(toast);
       }

    }

here is HTML

    <template>

    </template>

here is metaxml

     <?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>
      <target>lightning__AppPage</target>
      <target>lightning__HomePage</target>
     </targets>
    </LightningComponentBundle>

Solution

  • Few ways to do it.

    1. Ignore LWC completely. Does it have to be a "toast" at all? You could drop to the page a Rich Text Area field, put some text in red and use conditional display rules. Job done, zero code.

    enter image description here

    1. Similar - still use your component as is, but use the component visibility rules to make it display (run) only on New Cases.

    2. Edit your component to be something like this

    import { LightningElement, api, wire } from 'lwc';
    import { ShowToastEvent } from 'lightning/platformShowToastEvent';
    import { getRecord } from 'lightning/uiRecordApi';
    import CASE_STATUS_FIELD from '@salesforce/schema/Case.Status';
    
    export default class CaseTypeInformation extends LightningElement {
        @api recordId;
    
        @wire(getRecord, { recordId: '$recordId', fields: [CASE_STATUS_FIELD] }) wiredCase({ error, data }){
            if(data && data.fields.Status.value === 'New'){
                this.dispatchEvent(new ShowToastEvent({
                    'title': 'Case Type Level 1, level 2 and level 3 fields ',
                    'message': 'must be selected before saving'
                }));
            }
        }
    }
    

    enter image description here