I have an action on Contact page which opens a lightning component. How do I pass the current Contact record id to the lightning component?
2 Answers
You can refer to the documentation here
basically, you have implement the force:hasRecordId attribute:
<aura:component controller="yourControllerifany"
implements="force:hasRecordId">
declare it in your component:
<aura:attribute name="recordId" type="Id" />
and in your client side controller, you should be able to fetch the record ID:
"recordId": component.get("v.recordId")
further documentation on using actions and recordid's can be found here
-
2No need to declare an aura:attribute separately, if
force:hasRecordId
is implemented<aura:attribute name="recordId" type="Id" />
is inherited automatically– PraveenCommented Apr 11, 2017 at 18:54 -
I see, Good to know! i just noticed it is mentioned in the second documentation link I referred, however, the salesforce doc includes Both, the aura attribute and the force:recordId in the code snippet examples.– gllsCommented Apr 11, 2017 at 18:58
The best Possible workaround:
Component:
<aura:component implements="flexipage:availableForRecordHome,flexipage:availableForAllPageTypes,force:hasRecordId,lightning:actionOverride,lightning:isUrlAddressable">
<aura:attribute name="recordId" type="Id"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>
Controller:
({
doInit : function(component, event, helper) {
var pageRef = component.get("v.pageReference");
var state = pageRef.state; // state holds any query params
var base64Context = state.inContextOfRef;
/*
*For some reason, the string starts with "1.", if somebody knows why,
*this solution could be better generalized.
*/
if (base64Context.startsWith("1\.")) {
base64Context = base64Context.substring(2);
}
var addressableContext = JSON.parse(window.atob(base64Context));
component.set("v.recordId", addressableContext.attributes.recordId);
})
It is because of the context is changing when you overridden the standard button.