Saturday, May 18, 2019

How to override standard new button in Salesforce Lightning?

 Lightning:actionOverride interface enables a component to be used as an override for a standard action. We can override the View, New, Edit, and Tab standard actions on most standard and all custom components. The component needs to implement the interface Lightning:actionOverride
after which the component appears in the Lightning Component Bundle menu of an object action override properties panel.

Sample example:

<aura:component implements="lightning:actionOverride">

</aura:component>



Scenario:  Override the standard new button action on the contact object.

Solution:

1) Component: (Name=Contactinformation)


<aura:component controller="ContactInsertClass" implements="force:hasRecordId,flexipage:availableForAllPageTypes,lightning:actionOverride">
    <aura:attribute name="firstName" type="string"/>
    <aura:attribute name="lastName" type="string"/>
    <lightning:input label="Enter First Name" value="{!v.firstName}"/>
    <lightning:input label="Enter Last Name" value="{!v.lastName}"/>
    <lightning:button label="Add contact" onclick="{!c.addContact}"/>

</aura:component>

2) Javascript controller:


({
 addContact : function(component, event, helper) {
        alert('1111');
  var fName=component.get("v.firstName");
        var lName=component.get("v.lastName");
        var action=component.get('c.insertContact');
        action.setParams({
         
            parentAccountId:component.get("v.recordId"),
            firstName1:component.get("v.firstName"),
            lastName1:component.get("v.lastName")
           
        });
        action.setCallback(this,function(response){
            var state=response.getState();
            if(state==="SUCCESS")
            {
                alert('Contact inserted successfully');               
            }
           
        });
        $A.enqueueAction(action);
       
 }

})


3) Apex controller:


public class ContactInsertClass {
@AuraEnabled
public static contact insertContact(string parentAccountId,string firstName1,string lastName1)
{
    system.debug('Test');
    contact con=new contact();
    con.firstName=firstName1;
    con.lastName=lastName1;
    con.accountid=parentAccountId;
    insert con;
    return con;
   
}
}

Now, to override the standard new button action Go to >SETUP > OBJECT MANAGER > Buttons, Links, and Actions > Goto New > Click Edit.

Override the Lightning experience and Mobile view with the Lightning component.

Lightning:actionOverride

6 comments:

  1. Thanks for posing all the different scenarios.
    While going through the code in many examples, I observed that you have used extra variables or syntax which was actually not needed. Experienced programmer will understand this but the beginners will surely get confused.
    I referred this site to one of my friend who is trying to learn salesforce development but he got confused at many places due to extra declaration or unnecessary lines of code.

    ReplyDelete
  2. Thanks Saurabh, I will surely take your comments into consideration going forward.

    ReplyDelete
  3. i have confused with extra variable did you removed that extra variable which saurabh is talking about

    ReplyDelete
    Replies
    1. The comment was in general, this post is fine. We are trying to work on removing those extra parameters or variable which are not needed in other posts.

      Delete
  4. Thanks saurabh.

    When we click on new button.it is opening new page and created contact with out associate account object. Am I doing any thing wrong?

    ReplyDelete
  5. hey how can we redirect to view page after saving record?

    ReplyDelete