0

I've got a LWC embedded on a lightning page for the SBQQ__Quote__c object. It's subscribing to a platform event channel. I have a trigger setup on Opportunity (parent object of SBQQ__Quote__c) that's publishing the platform event based on certain criteria. Is there a way to limit the platform event subscription in the LWC so that it will only subscribe to the platform event if the event is related to the parent Opportunity record? Currently if I trigger the event from an Opportunity and I have unrelated Quote records open, they're all subscribing to the event. Ideally I'd like to have only Quotes related to the Opportunity be subscribed. If this isn't possible, maybe there's a way to filter within the subscribe method so that the callback doesn't run for these unrelated Quotes? I'm thinking possibly something along the lines of setting a Text field on the PE while publishing that will get stamped with the current Opportunity ID, then referencing this value inside of the LWC (ie: If the Quote's OppId == the Opp ID field on the PE). I had been trying similar attempts at this a few days ago and was unsuccessful, so any advice would be really helpful.

Here's my platform event functionality inside of the LWC:

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

export default class OpportunityContactRoles extends LightningElement {
    subscription = {};
    @api channelName1 = '/event/PO_Received_Message__e';

    connectedCallback() {
        this.handleSubscribe();
    }

    disconnectedCallback() {
        this.handleUnsubscribe();
    }

    handleSubscribe() {
        const callbackMessage = (response) => {
            this.displayStopSign = true;
            updateQuoteRecord({quoteId: this.recordId}).then((response) => {
            this.booleanVariable = true;
            this.dispatchEvent(new RefreshEvent());
            })
        }

        subscribe(this.channelName1, -1, callbackMessage).then(response => {
            console.log('Subscription request sent to: ', JSON.stringify(response.channel));
        })
    }

    handleUnsubscribe() {
        unsubscribe(this.subscription, response => {
            console.log('unsubscribe() response: ', JSON.stringify(response));
        });
    }
}
2
  • I've used the pattern you suggest of matching an Id included in the Platform Event with an Id known to the LWC successfully in the past. AFAIK that is the only possible approach.
    – Keith C
    Commented Mar 20 at 20:05
  • 1
    @Keith C - Thanks for confirming. I must have just been doing something wrong initially because I ended up getting it working with this exact approach. Going to add it as an official answer in case it could help others struggling with a similar issue in the future.
    – mcg123
    Commented Apr 26 at 20:27

1 Answer 1

1

I must have been doing something wrong initially because I ended up getting this to work via the solution I had mentioned in the question (as well as @Keith C's confirmation).

On my platform event object, I've created a custom field with an api name of 'Opportunity_Id__c'. It's just a text(18) field. Then within my trigger helper - When I'm creating the platform event, I'm setting this field's value to that of the current record's Opportunity ID (current record in the trigger is a CPQ Quote or CPQ Quote list). Keep in mind the code below isn't complete, I just want to show the part where I'm setting OpportunityId on the platform event:

            for(SBQQ__Quote__c platformEventQuote: platformEventQuoteList) {
                PO_Received_Message__e poReceivedEventMessage = new PO_Received_Message__e();
                poReceivedEventMessage.Opportunity_Id__c = platformEventQuote.SBQQ__Opportunity2__c;
                poReceivedEventMessage.message__c = 'testing';
                poReceivedEventMessageList.add(poReceivedEventMessage);
            }

And then within my LWC, I've got the subscribe method setup so that I'm only executing specific functionality if the current record's Opportunity ID matches the value of the Opportunity_Id__c found on the platform event:

if(obj.data.payload.Opportunity_Id__c == this.oppId) {
        this.displayStopSign = true;
        updateQuoteRecord({recordId: this.oppId}).then((response) => {
          this.booleanVariable = true;
          this.dispatchEvent(new RefreshEvent());
          })
        }

You must log in to answer this question.

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