Platform Event usage to refresh lwc screen - after featur method execution for record update auto refresh view

create one platform event


Publish platform event using apex

 EventBus.publish(new CaseScreenUpdateEvent__e(PublishScreenRefresh__c=true)


subscribe in lwc to refresh screen


HTML code :

<template>
  
  <div class="slds-section slds-is-open">
      <h3 class="slds-section__title">
        <button aria-controls="expando-unique-id" aria-expanded="true" class="slds-button slds-section__title-action">
        <!-- <svg class="slds-section__title-action-icon slds-button__icon slds-button__icon_left" aria-hidden="true">
            <use xlink:href="/assets/icons/utility-sprite/svg/symbols.svg#switch"></use>
          </svg>-->
          <span class="slds-truncate" title="Section Title">SLA Information</span>
        </button>
      </h3>
      <div class="slds-section__content" id="expando-unique-id">
        <!-- Section Body goes here {objectApiName} -->
        <!-- Border Line --><div class="slds-box slds-p-around_none slds-m-top_x-small slds-m-bottom_medium slds-m-horizontal_none">
          
          <lightning-record-form
              object-api-name="Case"
              record-id={recordId}
              fields={fields}
              onsuccess={handleSuccess}
          >
          </lightning-record-form>
<!-- Border Line -->

        </div>

         <!-- Section Body goes here-->
      </div>
    </div>
    <br>
    <br>
  
</template>




Js code :


import { LightningElement,api,track } from 'lwc';
import EntitlementId_FIELD from '@salesforce/schema/Case.EntitlementId';
import SlaStartDate_FIELD from '@salesforce/schema/Case.SlaStartDate';
import EntitlementProcessEndTime_FIELD from '@salesforce/schema/Case.CC_EntitlementProcessEndTime__c';
import MilestoneStatus_FIELD from '@salesforce/schema/Case.CC_MilestoneStatus__c';
import IsStopped_FIELD from '@salesforce/schema/Case.IsStopped';
import StoppedReason_FIELD from '@salesforce/schema/Case.CC_StoppedReason__c';
import StopStartDate_FIELD from '@salesforce/schema/Case.StopStartDate';
import SLAFailureReason_FIELD from '@salesforce/schema/Case.CC_SLAFailureReason__c';
import SLAAdditionalDetail_FIELD from '@salesforce/schema/Case.CC_SLAAdditionalDetail__c';
import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { subscribe, unsubscribe } from 'lightning/empApi';
import { refreshApex } from '@salesforce/apex';



export default class SLAInformation extends LightningElement {
   
   fields = [EntitlementId_FIELD,SlaStartDate_FIELD,EntitlementProcessEndTime_FIELD,MilestoneStatus_FIELD,IsStopped_FIELD,StoppedReason_FIELD,StopStartDate_FIELD,SLAFailureReason_FIELD,SLAAdditionalDetail_FIELD];
   @api recordId;
    subscription = {};
    channelName = '/event/CaseScreenUpdateEvent__e';
    handleSuccess(){
        console.log('On Save');
    }
    connectedCallback() {
        this.handleSubscribe();
    }

    disconnectedCallback() {
        this.handleUnsubscribe();
    }

    handleSubscribe() {
        const messageCallback = (response) => {
            this.refreshRecord();
        };

        subscribe(this.channelName, -1, messageCallback).then((response) => {
            this.subscription = response;
            console.log('On Subscribe');
        });
    }

    handleUnsubscribe() {
        unsubscribe(this.subscription, (response) => {});
    }

    refreshRecord() {
        console.log('Final Refresh');
        
        notifyRecordUpdateAvailable([{ recordId: this.recordId }]);
            refreshApex(this.recordId);

        
    }

 }




meta.xml code :



<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>59.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>





Referel video for platform events youtube series :







Comments

Popular posts from this blog

How to get sessionid and salesforce org base url in salesforce

salesforce - sf - sfdx - cli - commands

Multi currency in Salesforce - Revenue Cloud Advance - Revenue Lifecycle Management