8

I have a lightning component that is available for use as lightning component quick action.

I need to use this component from various object actions, for example, I will add a lightning component quick action to account object and to contact object.

Now on the lightning component itself, I want to know if it was invoked from the account object or from the contact object, but I cannot find a way to do that.

I have found a way to get the record Id by adding <aura:attribute name="recordId" type="String" /> (see code snippet below)

It is working but not good enough.

What I really need is to send a parameter from the lightning component quick action invocation: enter image description here

And get it in my component:

<aura:component implements="force:lightningQuickAction,force:hasRecordId,flexipage:availableForAllPageTypes">
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="param" type="String"/>
</aura:component>

Is this possible?

And if not - any workaround for achieving that?

14
  • I don't think you can. Can't you query it without h the record id ? Jan 10, 2017 at 16:18
  • query what? how do I query the quick action that invoked the component? Jan 10, 2017 at 19:33
  • Sorry for being unclear. What kind of parameter do you want to add ? It's not a value related to your contact record ? Jan 11, 2017 at 10:05
  • It's not related to the contact record. I want to pass the button name Jan 11, 2017 at 10:39
  • 1
    Ok, so I don't think you can do it. If you want to use the same component on 2 different buttons, I guess you'll need to create 2 separates wrapper components, one for each button. You'll be able to use your initial components with different attributes this way. Not exactly what you wanted but I don't see any other way for now. Jan 11, 2017 at 14:46

6 Answers 6

2

A hacky workaround to pass one parameter (besides record Id) is to name the quick action API name like so: ParamVal_XXX_QuickActionName

Example: a quick action with API Name Foo_XXX_Quickie created on Account object

Then in your LWC, you can parse the Param Value off the quick action API name like so:

import { api, wire } from "lwc";
import { CurrentPageReference } from "lightning/navigation";
............

@wire(CurrentPageReference)
parseParam(CurrentPageReference){
    if (currentPageReference.type === "standard__quickAction") {
        let quickActionPath = currentPageReference.attributes.apiName; // Account.Foo_XXX_Quickie
        let paramValue = quickActionPath.split(".")[1].split("_XXX_")[0]; // Foo
    }
}
2
  • A nice workaround @snippet69, although it is not exactly sending params, it can differentiate between the different actions the are using the LWC. thank you for sharing this! Mar 22, 2023 at 13:59
  • I have added this to my Quick Action LWC but it is only returning the API Name of the record page - not the Quick Action. Oct 28, 2023 at 2:41
5

I don't think you can do it. If you want to use the same component on 2 different buttons, you'll need to create 2 separates wrapper components, one for each button. You'll be able to use your initial component with different attributes this way. Not exactly what you wanted but I don't see any other way for now.

2
1

This is absolutely possible, you will need to use apex to get the object type from the recordId.

From there you can pass it back to the JS controller and then decide on what to do with it then.

See here: https://help.salesforce.com/articleView?id=How-to-find-Object-Type-from-Record-ID-Prefix&language=en_US&type=1

1

You can use the "lightning:isUrlAddressable" to access a lightning component using an url and create a "Detail Page Link" with behavior as "Display in existing window without sidebar or header" that directs you to the component URL, as mentioned in the documentation:

https://developer.salesforce.com/docs/component-library/bundle/lightning:isUrlAddressable/documentation

The URL in the detail link should look like below:

{!URLFOR('/lightning/cmp/namespace__componentName', null, [param=value])}

NOTE: Look for the detail link in the "Custom Links" section of the layout.

0

I don't know if I understood correctly. But if you wanna know which object is calling the lightning component, you could check its Id's prefix. Each object has its own Id prefix, por instance:

ID Prefix -> Object Type:

001 -> Account

002 -> Note

003 -> Contact

Or you could use this method to get the object name by its Id:

Id sampleid = '00561000000Mjya';

System.debug('object is '+ sampleid.getsobjecttype());

So, you could pass any object's Id as parameter to the lightning component, and check its Id prefix to get the correct object and change the logic accordingly.

1
  • And just how do you pass the Object's Id from a Quick Action? That's the whole point of the question!
    – cyberspy
    Oct 11, 2022 at 14:54
0

You can add force:hasSObjectName to your component implements list and then use that in your doInit

Add to your component:

<aura:attribute name="sobjecttype" type="String" default="{!v.sObjectName}"/>
<aura:attribute name="recordId" type="String"/>

And in your doInit:

({doInit: function(component, event, helper){        
    var recId = component.get("v.recordId");
    var sObjectType = component.get("v.sobjecttype");

    if(recId && sObjectType === 'Account') {}
    if(recId && sObjectType === 'Contact') {}        
}})
2
  • 2
    Your solution will work only for the object type. But I need other parameters to send, not the object type Oct 18, 2018 at 20:10
  • @ItaiShmida My solution covers one part of your question: " Now on the lightning component itself, I want to know if it was invoked from the account object or from the contact object, but I cannot find a way to do that." Regarding other parameters, can you not query the server side and pass the recordId as argument? You should be able to retrieve any params you want.
    – Alexandru
    Oct 18, 2018 at 21:13

You must log in to answer this question.

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