function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddharth ManiSiddharth Mani 

Override Standard New button with a Lightning Component and VF Page

I have a requirement to prepopulate some fields (Address Fields) when standard "New" button is clicked from the related list (Contact) of Account. This was being achieved using an intermediate VF page in classic.
Now in lightning, I am able to create a component with a quick action to achieve the same - but the problem here is that the override has to be either a vf page or a lightning component.
Is there any way to have both on a single button - like when in classic, follow the old approach by redirecting to an intermediate vf and prepopulating the values and when in lightning, use the newly created lightning component?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Siddharth,

Your component needs to implement lightning:actionOverride.please refer the below link for reference. hope it helps.

Please mark it as best answer if the information is informative

Rahul Kumar
Siddharth ManiSiddharth Mani
Hey Rahul,

That part is already done - not sure if my question is clear or not. I want a vf page when i click on "New" button in classic and a lightning component when in lightning exp. Both are needed and not one. I am able to do both of these individually, but I need it to work seamlessly. Hope you get my point!!!
Let me know in case any other info is needed from me!
Sriram PenkeySriram Penkey

Hi Siddharth, I am in similar situation but facing a different problem.

I am not able to get Account Id when I try to invoke lightning component using Overridden "New" Button from related list like Contact/Calls.

Were you able to get Account Id when using lightning component??


Thanks, Sriram

Siddharth ManiSiddharth Mani
Hey Sriram - you wont be able to get the record id if you are invoking the button from related list as the context of the current record is lost. We ended up removing the related list button altogether and created a quick action for the same which would appear on top like a detail page button. You can try to read the current page URL using your component JS controller to get the ID if you really need it to work from related list. Otherwise I dont think there's an option to do this (Maybe a VF based approach could also work!!!!).
Pramodh KumarPramodh Kumar
@Siddharth Mani

you cannot differentiate actions in the salesforce. You can either override with vfpage or lightning component.

In your scenario create a vfpage using themes and SLDS for the lightning side.

here is the small example for you 

Theme3​ is Classic
Theme4d​ is lightning.
<apex:stylesheet value="{!URLFOR($Resource.slds, 'assets/styles/salesforce-lightning-design-system-vf.css')}" />
    <apex:outputPanel rendered="{!$User.UIThemeDisplayed == 'Theme3'}">
        <apex:pageBlock title="Visualforce (Using standard Visualforce Components)" mode="detail">
            <apex:outputLabel value="UI Theme Detection using Global Variables: {!$User.UIThemeDisplayed}"/>
    <apex:outputPanel rendered="{!$User.UIThemeDisplayed == 'Theme4d'}">
       <div class="slds">
    <div class="slds-card slds-card--empty">
  <div class="slds-card__header slds-grid grid--flex-spread">
    <h2 class="slds-text-heading--small slds-truncate">Visualforce (Using HTML + SLDS)</h2>
  <div class="slds-card__body slds-p-horizontal--small">
    <h3 class="slds-text-heading--small slds-p-top--large slds-p-bottom--large">UI Theme Detection using Global Variables: {!$User.UIThemeDisplayed}</h3>

Siddharth ManiSiddharth Mani
Hi Pramodh,

Thanks for the suggestion. I did try out this approach, but the problem I faced was that I was unable to get the current record Id from where the button was being clicked when in lightning. Without getting the current record Id, it wont be possible to pre-populate fields which is ultimately the requirement. Hence went for a Quick action+component approach and removed the button from related list altogether.
Let me know if you are able to get the current record Id in lightning when you click a button from a related list?
Roman KudrinRoman Kudrin
To get Record ID if component was called from override New button i do workaround with RecentlyViewed table.
list<RecentlyViewed> lastViewedRecords = [
		            SELECT Id, Name, Type
		            FROM RecentlyViewed
		            WHERE LastViewedDate != null
		            ORDER BY LastViewedDate DESC limit 10];

		if (lastViewedRecords != null && lastViewedRecords.size() > 0 ) {
			if (lastViewedRecords[0].Type == 'Account') {
				list<Account> lastViewedAccount = [SELECT id, Name from Account where id = : lastViewedRecords[0].Id ];
If you found better way, please share.
kashish choudharykashish choudhary
For overiding the Standard action on the Detail Page You have to use a interface on lightning Component 
implements="lightning:actionOverride,force:hasRecordId,force:hasSObjectName"  By using that you can override that Standard Action.
You can Have a look on