1

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>
David Reed
  • 2,522
  • 2
  • 16
  • 16
Carolyn Cordeiro
  • 1,525
  • 3
  • 11
  • 26
  • What does "fired" mean? Where is this LWC exposed in the user interface? – David Reed Nov 17 '21 at 18:41
  • with fired meant LWC component is displayed on CASE page record ,so each time user opens case this LWC on case page record is displayed even on existing case records,need a way to make this component get displayed only on NEW case records – Carolyn Cordeiro Nov 17 '21 at 19:04
  • especially need this for say RecordType.Name = 'XXX' – Carolyn Cordeiro Nov 17 '21 at 19:29
  • Once the record is saved, it's not new. Are you overriding the Record Create action? – David Reed Nov 17 '21 at 20:05
  • No its a very basic LWC which displays justa a Toastmessage on Cases ,havent overridden any action up until now – Carolyn Cordeiro Nov 17 '21 at 20:40
  • I misunderstood: `New` is a Case Status value, not a newly-created record. You will need to use one of the strategies in the [Lightning Web Components Developer Guide](https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data) to access Salesforce data via imperative Apex or a wire service. – David Reed Nov 17 '21 at 21:31

1 Answers1

1

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

eyescream
  • 18,088
  • 2
  • 34
  • 46