0

I have the following flow, LWC -> Apex (REST API) -> Inserts data in some_object__c -> Trying to refresh the LWC which is based on the some_object__c.

I have a platform event(PE) which fires when data is inserted in the some_object__c via a process builder. I am subscribing to the PE in LWC using, once the PE is fired I can see the console log, upto this point everything is working, now if you see my commented out code, I am trying to query the apex class to give me the data from some_object__c, it is showing me old data and not the newly inserted data.

Question: How can I get the most recent data in my LWC ? I cannot use @wire that annotation fails.

import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';
import getSegmentMemberships from '@salesforce/apex/SegmentMembershipController.getSegmentMemberships';

    handleSubscribe() {
    // Callback invoked whenever a new event message is received
    const messageCallback = function(response) {
        console.log('New message received: ', JSON.stringify(response));
        // Response contains the payload of the new message received
        this.notifier = JSON.stringify(response);
        // getSegmentMemberships({})
        //     .then((data) => {
        //         if (data) {
        //             console.log(data);
        //             this.segmentName = data[0].Segment_Name__c;
        //             this.segmentMembershipList = data;
        //         }
        //     })
        //     .catch((error) => {
        //         this.error = error;
        //     })


    };

    // Invoke subscribe method of empApi. Pass reference to messageCallback
    subscribe(this.channelName, -1, messageCallback).then(response => {
        // Response contains the subscription information on subscribe call
        console.log('Subscription request sent to: ', JSON.stringify(response.channel));
        this.subscription = response;
    });
3
  • Why is your message callback inside, handleSubscribe(), that way it won't be available in subscribe method. Try adding a timeout of few seconds to rule out if this is an issue with timing.
    – Raul
    Commented Apr 12, 2021 at 16:52
  • What is the publish behavior of the platform event? You can check this in Platform event -> Navigate to your Platform event -> Edit -> Publish Behavior Commented Apr 13, 2021 at 3:44
  • @Raul I have not published my entire code, this.handleSubscribe() is invoked from the connectedCallback and I am seeing the PE JSON in my console, so subscription is also working it is that the imperative apex is not getting called. Commented Apr 16, 2021 at 18:52

1 Answer 1

1

I was able to successfully subscribe to the platform event channel using a arrow function instead of the a regular function. Please my changes below.

Before state : I could see the event payload, but nothing happened after that.

const messageCallback = function(response) {...}

After state : I can see the JSO payload also apex imperatives were firing in the messageCallback function.

const messageCallback = (response) => {...}

Hope it will help others !!!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .