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
punith narasimhapunith narasimha 

How to create a custom button in lightning component with out opening component page

Hi Friends 
I want to create a custom button in lead object in lightning component
when click on the custom button create a contact record 
but i dont want to open any component page or alert or model box 
just create a contact record these process done in lightning only



 
Ramakrishna Reddy GouniRamakrishna Reddy Gouni
If you create one aura component element then you create a button in another component and drag into your target component from edit flexi page where you use your target component. 
Otherwise, it is not possible to create direct button in your target component. 
Ajay K DubediAjay K Dubedi
Hi Punith,
Please add this component on the lead record page. Try the below link for add component on the record page. 
https://trailhead.salesforce.com/en/content/learn/modules/lightning_app_builder/lightning_app_builder_recordpage

Please try the below code and let me know if this works for you. If still need modifications do let me know.

Component:
<aura:component controller="Demo" implements="flexipage:availableForAllPageTypes">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:button variant="brand" label="Base" title="Base action" onclick="{!c.saveCon}" />
</aura:component>



Controller:
 
({
    saveCon:function (component, event, helper) {
        helper.setContact(component, event, helper);
    }
})



Helper:
 
setContact : function(component,event, helper) {
            var action = component.get("c.createCon");
        action.setParams({
        });
           action.setCallback(this, function (response) {
            if(response.getState() === 'SUCCESS') {
                var storedResponse = response.getReturnValue();
                
                console.log('storedResponse:');
                console.log(storedResponse);
            } else {
                console.log('ERROR');
                console.log('hii'+JSON.stringify(response.getError()));
            }
        });
        $A.enqueueAction(action);
    },
})



Apex Controller:
 
public Demo {
 @AuraEnabled
    public static Contact createCon(){
        Contact con = new Contact(LastName='test');
        insert con;
        return con;
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi