Saturday, May 18, 2019

Lightning:actionOverride

 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 appear 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 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

No comments:

Post a Comment