I have developed a lightning component modal popup to show on the opportunity page. There are two options Yes and No. On condition this lightning component is transferring the flow to one visualforce page or the other with the account id. How I can get the account id in the lightning component.
<aura:component implements="force:lightningQuickActionWithoutHeader">
Are you sure you want to proceed?
<div class="slds-align_absolute-center">
<lightning:button
label="No"
variant="destructive"
onclick="{!handleNo}"
></lightning:button>
<lightning:button label="Yes" onclick="{!c.handleYes}"></lightning:button>
</div>
</aura:component>
and the controller is
({
handleNo: function (component, event, helper) {
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
url: "/apex/MyOtherVisualforce",
isredirect: "true"
});
urlEvent.fire();
},
handleYes: function (component, event, helper) {
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
url: "/apex/MyVisualforce",
isredirect: "true"
});
urlEvent.fire();
}
});
In order to obtain the Account Id from the Opportunity, first you need to get the Opportunity Id where the Quick Action is being executed. That can be easily achieved by implementing the force:hasRecordId interface (in addition to the lightningQuickActionWithoutHeader one that you are already implementing). By doing this, you get access to recordId attribute which already contains the record id of the Opportunity in this case (https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation).
Once you get the Opportunity Id, you can use different methods to obtain the related Account's id. You can create an Apex Controller but you can also use the force:recordData component (https://developer.salesforce.com/docs/component-library/bundle/force:recordData/documentation) to obtain the Account Id.
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
<aura:attribute type="Opportunity" name="opportunity" />
<force:recordData
recordId="{!v.recordId}"
fields="AccountId"
targetFields="{!v.opportunity}"
/>
Are you sure you want to proceed?
<div class="slds-align_absolute-center">
<lightning:button
label="No"
variant="destructive"
onclick="{!handleNo}"
></lightning:button>
<lightning:button label="Yes" onclick="{!c.handleYes}"></lightning:button>
</div>
</aura:component>
Controller:
({
handleNo: function (component, event, helper) {
var accountId = component.get("v.opportunity.AccountId");
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
url: "/apex/MyOtherVisualforce",
isredirect: "true"
});
urlEvent.fire();
},
handleYes: function (component, event, helper) {
var accountId = component.get("v.opportunity.AccountId");
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
url: "/apex/MyVisualforce",
isredirect: "true"
});
urlEvent.fire();
}
});