Skip to main content

#Issues0 discussing

Preventing Recursive Lightning Message Service Calls in Screen Flow LWCs Using a Source Flag 

When working with Lightning Message Service (LMS) inside Screen Flow embedded LWCs, communication between components becomes extremely powerful — but it can also introduce unexpected recursive behaviour.

Recently, I encountered an interesting real-world scenario where two LWCs on a Screen Flow were unintentionally triggering each other repeatedly, causing incorrect value population even though the logic looked perfectly fine.

This blog walks through:

  •  The Scenario 
  •  The Problem 
  •  Debugging Process 
  •  Root Cause 
  •  The Solution (Using Source Flag) 
  •  Best Practices 
  •  Final Takeaways 

Scenario

I had a Screen Flow containing two Lightning Web Components:

  • LWC A — Publishes some values 
  • LWC B — Publishes some values 
  •  Both components subscribe to the same Lightning Message Channel

So essentially:

  •  LWC A publishes → LWC B receives 
  •  LWC B publishes → LWC A receives 

This setup is valid and expected — and initially, everything worked correctly.

However, as the use case evolved, values started getting populated incorrectly

 

The Issue

Even though:

  •  Logic was correct 
  •  Variables were correct 
  •  Message structure was correct 

The values were getting overwritten incorrectly.

Example Behavior:

  1.  LWC A publishes value 
  2.  LWC B receives and updates 
  3.  LWC B publishes updated value 
  4.  LWC A receives again 
  5.  LWC A publishes again 
  6.  Loop continues... 

This created a recursive publish-subscribe loop

 

Debugging Approach

To identify the issue:

  •  Added console logs 
  •  Checked publish events 
  •  Checked subscription callbacks 

While debugging, I noticed:

  • Publish method firing multiple times
  • Subscription handler triggering repeatedly
  • Unexpected value overwrites

This clearly indicated recursive message passing

 

Root Cause

The root cause was:

Since both components:

  •  Subscribe 
  •  Update values 
  •  Publish again 

It resulted in infinite cross-communication.

This is a common pitfall when using Lightning Message Service with multiple publishers

The Solution — Using a Source Flag

To stop recursion, I introduced a Source Flag inside the message payload.

The idea:

  •  Each LWC sends its name as source
  •  When receiving a message: 
    •  If message source == current LWC 
    •  Ignore the message 

This prevents recursive triggering.

Example Message Payload

{

value: selectedValue,

source: 'LWC_A'

}

Implementation Example

Publisher

publishMessage() {

const message = {

value: this.selectedValue,

source: 'LWC_A'

};

publish(this.messageContext, SAMPLEMC, message);

}

Subscriber

subscribeMessage() {

this.subscription = subscribe(

this.messageContext,

SAMPLEMC,

(message) => {

if(message.source === 'LWC_A') {

return;

}

this.selectedValue = message.value;

}

);

}

 

Now the flow becomes:

  1.  LWC A publishes (source = LWC A) 
  2.  LWC B receives 
  3.  LWC B updates UI 
  4.  LWC B publishes (source = LWC B) 
  5.  LWC A receives 
  6.  LWC A updates UI 
  7.  No recursion 

#Lightning Web Components  #Lightning Message Channel  #Salesforce Developer  #Issues

0/9000

There seems to be an error in the Trailhead Module Keep Data Secure in a Recruiting App.  The step for Restrict Data Access with Field-Level Security, Permission Sets, and Sharing Settings.  The error is attached along with a screenshot of the available settings.

12 comments
0/9000
5 answers
  1. Ines Garcia (get: Agile) Forum Ambassador
    Nov 16, 2023, 11:44 AM

    it may be email client side, have you looked at spam and or 'all mail' folders?

0/9000

Who should I contact to troubleshoot issues regarding my password/login info on Quip? 

1 comment
  1. Jan 7, 2022, 9:16 PM

    Hi,

    Has anyone responded to your question? I am trying to determine how to change my password in Quip and can''t find anything.

    Thx

0/9000

Hello,

 

I recently updated Chrome to Version 92.0.4515.107 and noticed that after the update I cannot update and run rules on any dynamic segmentation lists when using Pardot through Salesforce. I found that what is keeping the rules from running is that Chrome is blocking the message box popup that confirms that I want to re-run the rules on the list. After some research I found that with the latest update, Chrome has implemented a security feature that disallows cross-origin alert boxes in iframes.

 

https://www.chromestatus.com/feature/5148698084376576#details

 

We have found that the only workaround for this is to use Pardot directly from the original site.

 

If anyone knows of a better solution for this please let me know.

 

Thanks,

 

https://www.chromestatus.com/feature/5148698084376576#details

 

#Pardot

#Issues

9 answers
0/9000

Has anyone experienced issues with trying to add a custom font to a community? I have followed the instructions Salesforce outlined at the link below, but I have been unable to see the changes reflected in my published community. However, I do see them on my end/laptop...just nowhere else.

 

https://developer.salesforce.com/docs/atlas.en-us.communities_dev.meta/communities_dev/communities_dev_customize_font.htm

1 comment
  1. Jul 14, 2021, 12:42 AM

    I am struggling with this as well. I've used this CSS in the global CSS:

     

    @font-face {

    font-family:'Optimist';

    src: url('/sfsites/c/resource/Optimist') content('TrueType');

    }

    This works for Firefox and Chrome but not for Safari. Note that using format('TrueType') doesn't work. Is there a solution for this?

0/9000

Anyone else have Sandboxes with summer '21 and issues with Search not being responsive or working? Finding issues in the global search even in search within list views etc... like you start to type then it doesn't do anything or starts to put in a few letters then doesn't allow more.  

4 comments
  1. Jun 23, 2021, 6:58 PM

    Having issues in Live with Global search - last 2 days - particularly cases?  Working fine for admin but some users are unable to see some csaes

0/9000

Hello,

 

I am suddenly seeing issues on dependency packages on some of the branches while trying to push into scratch orgs. https://gist.github.com/Sandhya-Rakesh/5aa3b1eaa68ca5f20c78ae09708c81ea

 

Thanks!

4 comments
  1. Jun 4, 2021, 5:18 AM

    I get this a lot when simply working on my machine at home. Some things that have helped:

    Turning of QOS on your router - this priorities Media traffic and i notice it happens more often when it is on. (I am not a network person.)

    I have had more success with Google DNS than my standard DNS from my provider.

    But the biggest one that gives me stability is using a VPN that connects to the closest city to my where my orgs are based.

0/9000

Hello everyone,

I'm facing a issue when I'm attaching a file on an object record. When I'm uploading file from related list that file is uploaded successfully but file count is not getting increased on Related List Quick Links for File. Page reload is required to get right count of files.

 

  •  

0/9000