1

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?

2 Answers 2

1

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.

2
  • If I understand you correctly, you're saying make 3 separate LWC components for each Action and have each of those components load the "Main" component into their wrapper component while passing a parameter to the main component? Mar 9 at 17:13
  • Yes. That main component can change behavior based on that additional parameter that wrapper components passed.
    – metasync
    Mar 9 at 18:04
2

This worked for me to grab the API name of the quick action. It uses the importation of CurrentPageReference from lightning/navigation.

import { LightningElement, wire } from "lwc";
import { NavigationMixin } from "lightning/navigation";
import { CurrentPageReference } from "lightning/navigation";

export default class MyComponent extends NavigationMixin(
    LightningElement
) {
    quickActionAPIName = "";

    @wire(CurrentPageReference)
    getStateParameters(currentPageReference) {
        if (currentPageReference.type === "standard__quickAction") {
            let quickActionPath = currentPageReference.attributes.apiName; // ex: Opportunity.My_Quick_Action
            this.quickActionAPIName = quickActionPath.split('.')[1]; // Ex: My_Quick_Action
        }
    }
}
1
  • +1 Nice solution!
    – Raul
    Apr 30 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.