6

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?

  • I don't think you can. Can't you query it without h the record id ? – Fabien Taillon Jan 10 '17 at 16:18
  • query what? how do I query the quick action that invoked the component? – Itai Shmida Jan 10 '17 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 ? – Fabien Taillon Jan 11 '17 at 10:05
  • It's not related to the contact record. I want to pass the button name – Itai Shmida Jan 11 '17 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. – Fabien Taillon Jan 11 '17 at 14:46
4

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.

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

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.

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
    Your solution will work only for the object type. But I need other parameters to send, not the object type – Itai Shmida Oct 18 '18 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 '18 at 21:13
0

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.

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.