function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Darshit Pathak 10Darshit Pathak 10 

Data not refreshed with notifiyRecordUpdateAvailable after platform Event

Can someone look into this code and share what's wrong in this code?

LWC is hosted on an App Page. it shows the old Account name only unless page is refreshed, even after Account name is updated from Account Detail Page.

LWC.html

<template>
   <template lwc:if={accRec}>
      Acc Name: {name}
   </template>
</template>

LWC.js
import { getRecord, getFieldValue,notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import ACC_NAME from '@salesforce/schema/Account.Name';
const fields = [ACC_NAME];
export default class Test_LWC extends LightningElement {
    recordId='0012w00001nF5DIAA0';
    @track accRec;
    @wire(getRecord,{recordId:'$recordId',fields})
    accRecFunc(result) {
        if(result.data != undefined) {
            this.accRec = result.data;
        }
    }
    get name() {
        return getFieldValue(this.accRec.data, ACC_NAME);
    }
channelName = '/event/Event1__e';
   
    connectedCallback() {
        this.subscribeEvent();
    }
    subscribeEvent() {
        const messageCallback = function (response) {
            console.log('in callback: '+JSON.stringify(response));
            console.log('before Notify');
            notifyRecordUpdateAvailable([{recordId:this.recordId}])
            .then((result) => {
                console.log('in notify then: '+JSON.stringify(result));
            })
            .catch((error)=> {
                console.log('in notify error: '+JSON.stringify(result));
            });
            console.log('after Notify');
        };
        subscribe(this.channelName, -1,messageCallback).then((response) => {
            console.log('in subscribe : '+JSON.stringify(response));
        });
    }
}

Account Trigger
trigger AccountTrigg on Account (after update) {
if (Trigger.isAfter) {
    Event1__e ev1 = new Event1__e();
    EventBus.publish(ev1);
}
}

Best Answer chosen by Darshit Pathak 10
Darshit Pathak 10Darshit Pathak 10

Hi Ashwini,
Thanks for your response.
Platform event was fired properly as console.log('inside of callback: ' + JSON.stringify(response)); was being printed properly.
Later on I found the what was the issue, that when platform event is received in LWC, "this" has lost the context and this.recordId doesnt return anything.
so writing  let self = this;  as a first line in subscribeEvent() method, and then using "self.recordId" will fix the issue.

All Answers

AshwiniAshwini (Salesforce Developers) 
Hi Darshit,
Issue seems to be with the scoping of this.recordId in your event callback function. 
To access the recordId, you can use an arrow function to maintain the correct scope. 
const messageCallback = (response) => {
    console.log('inside of callback: ' + JSON.stringify(response));
    console.log('Notify Before');
    notifyRecordUpdateAvailable([{ recordId: this.recordId }])
        .then((result) => {
            console.log('inside notify then: ' + JSON.stringify(result));
        })
        .catch((error) => {
            console.log('inside notify error: ' + JSON.stringify(result));
        });
    console.log('after Notify');
};

Also verify that your platform event is triggered accurately by the Account trigger upon Account updates and only published when necessary data changes to prevent unnecessary notifications.

Related:https://www.sfdcblogs.com/post/arrow-function-in-lightning-web-component-lwc 

If this information helps, please mark the answer as best. Thank you
 
Darshit Pathak 10Darshit Pathak 10

Hi Ashwini,
Thanks for your response.
Platform event was fired properly as console.log('inside of callback: ' + JSON.stringify(response)); was being printed properly.
Later on I found the what was the issue, that when platform event is received in LWC, "this" has lost the context and this.recordId doesnt return anything.
so writing  let self = this;  as a first line in subscribeEvent() method, and then using "self.recordId" will fix the issue.

This was selected as the best answer