LWC Quick Action – Can we determine which Action button invoked the LWC component

dynamiclightning-web-componentsquickactionredirecturl

I've written a LWC Quick Action with corresponding JavaScript & Apex Methods. I want this component to be invokable from 3 separate Action Buttons on a record page. In all 3 actions, I want it to execute all of the same business logic with the exception of the final redirect to URL step. Each action should re-direct to a separate URL.

To do this, I believe I'd need a way to determine which action button invoked the LWC component so that I could dynamically render the appropriate redirect URL before passing it to the Navigation MixIn. As of now, I've been unable to find any reference in the documentation for this type of feature.

I did find that the Action Button name shows up in the URL string after you click on the action (`https://MyDomain.lightning.force.com/lightning/action/quick/Object_Name__c.Action_Name?etc)

Since the Object_Name__c.Action_Name reference in the URL precedes the URL parameters section, I can't easily grab the parameter, so the only option I see available to me right now would be to use window.location.href and pull the value out by assuming that the URL structure will always end in /action/quick/VALUE_HERE.AND_HERE?

This of course feels like a hack that could break in the future. Is there a better/more supported way for me to do this, or is this my only option with what's available today?

Best Answer

LWC component is typically not allowed to know the caller unless the information is passed as an attribute. While the hack you mentioned might work but sure will fail if sfdc makes changes to url format.

Better approach would be, create a base LWC component with core of the logic, and create 3 additional wrapper components that quick actions will invoke. That wrapper component can pass an attribute to core lwc component which it can use to redirect or change the behavior.

Related Topic