Skip to main content
So I am strugglign with trying to get the record Id in the web component below. Basically, I need to compare the Id I am getting from my platform event to the Id of the current record. I am trying to use the @api deocrator, but when I try to reference the $recordId property, in the If statment condition, it is coming up as underfined.

{code:javaScript}

import { LightningElement, track, api } from 'lwc';

import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';

import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class EmpApiLWC extends LightningElement {

@track channelName = '/event/Notify__e';

@api recordId;

subscription = {};

// Handles subscribe button click

connectedCallback() {

    // Callback invoked whenever a new event message is received

    const messageCallback = function(response) {

        console.log('New message received : ', JSON.stringify(response));

        console.log(response.data.payload.RecordId__c);

        console.log(caseId);

   if(response.data.payload.RecordId__c=== '$recordId'){

        const event = new ShowToastEvent({

                title: 'Record Updated',

                message: 'This Record has been updated by' + response.data.payload.LastModBy__c+ 'and is Owned by' + response.data.payload.Owner__c,

        // Response contains the payload of the new message received

    });

    this.dispatchEvent(event)

}

        // Response contains the payload of the new message received

    };

    // Invoke subscribe method of empApi. Pass reference to messageCallback

    subscribe(this.channelName, -1, messageCallback).then(response => {

        // Response contains the subscription information on successful subscribe call

        console.log('Successfully subscribed to : ', JSON.stringify(response.channel));

        this.subscription = response;

    });

    

}

{code}
9 respuestas
  1. 26 dic 2023, 4:15

    Hi, @Marilyn Baculima

    I also encounter this issue and solved this. here's my idea.

     

    The reason why recordId is undefined is that seems salesforce launch a new context for the messageCallback function. So we should define and pass them to the new variable and use this. below is my code and it works(already tested)Hi, I also encounter this issue and solved this. here's my idea. The reason why recordId is undefined is that seems salesforce launch a new context for the messageCallback function.also you can see the whole code which from my blog: https://www.cnblogs.com/zero-zyq/p/17927154.html

     

    Please mark this as best solution to help more developers.

0/9000