I'm trying to override the new button on one of my objects with a lightning component. If this object is being created on an opportunity is there a way to get the id of that opportunity? If you override the new button with a visualforce page you can access the opportunity id in the url, so I'm just curious if theres a similar way to get the opp id with a lightning component.

  • I have started development on my component. You mentioned it is possible, what are some good resources on how to go about this? – SalesforceUser1234 Mar 29 '18 at 17:45
  • I just made a new button for casecomments from case object, I used force:hasrecordid in order to grab the case id and place it into the casecomment parentid field – Tim Willis Mar 29 '18 at 17:47

I am assuming you are trying to create a new child record on an Opportunity, using the New button on related list.

Your lightning component must implement lightning:actionOverride and force:hasRecordId interface. You must also manually override the New button on the child object with the lightning component you created.

Doing so, Salesforce will automatically add a recordId attribute on your component, with the Opportunity record Id.

In your client side controller or helper, you can fetch the Opportunity record id using component.get("v.recordId").

Whenever a lightning component is embedded on the lightning record detail or page, lightning automatically adds the recordId in context, provided the hasRecordId interface has been implemented.

Salesforce document states that:

The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home.

  • 1
    I tried this, but for me it does not work. I did an override on the 'new Case' button. Implemented the Lightning Component with actionOverride and hasRecordId. Then when I click on the 'New Case' button on the Account layout, there is no recordId to be found. It works however when I embed the Lightning Component directly on the Account Lightning Page, but that's not what we want. Are you sure your suggested solution works? – Willem Mulder Dec 3 '18 at 15:53
  • 1
    You actually cant use force:hasRecordId with an overridden "new" button. If you try overriding the "new" opportunity button on an Account related list, you'll find that component.get("v.recordId") is null. I can't tell you why, but this is definitely a fact. – Nicholas Zozaya Mar 15 at 6:42

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.