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
Mohammed AzarudeenMohammed Azarudeen 

Prepopulate Account name when creating contact using Lightning Component

I have created a lightning component form to insert Contact record which is having Account Name, First Name, Last Name, Email and all the necessary form fields.

I've overridden the Contact object "New" button with this lightning component.

Consider am currently on the Account Detail page, and scrolling down to click on New button of the contact related list. Now, when the lightning component form is loading, the account name from which I redirected should be populated automatically.

This can be easily done in button if the content type is URL. But how do we achieve the same in lightning component content type?

Any ideas would be appreciated. Thanks!.
Naveen PoojaryNaveen Poojary
Hi Mohammed Azarudeen,
You can refer my code to create a related contact on account related page.Here I made it for First name and Last Name only. Please make it as Best answer if i solves your issue.
<aura:component controller="CreateRelatedcontact" 
                access="global" >
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="Name" type="String"/>
    <aura:attribute name="account" type="Account"/>
    <aura:attribute name="contact" type="Contact"
                    default="{ 'sobjectType': 'Contact','LastName':'','FirstName':'','Name'	:'','AccountId':''}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    <div class="slds-form-element__control">
        <lightning:input name="Account Name"  label="Account Name" value="{!v.account.Name}" disabled="true"/>
    <div class="slds-form-element__control">
        FirstName  <force:inputField value="{!}"/>
    <div class="slds-form-element__control">
        LastName  <force:inputField value="{!}"/>
    <lightning:button aura:id="insert" label="Create Contact" class="slds-button slds-button--brand buttontype" onclick="{!c.InsertCont}" />  

<!-- Controller-->
    doInit : function(component, event, helper) {
        var accid = component.get("v.recordId");
        var action=component.get("c.getAccount");
            acid : component.get("v.recordId")            
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS"){
    InsertCont : function(component){
        var action=component.get("c.insertCon");
        action.setParams({ "newcon": component.get("") });
        action.setCallback(this, function(response)
                               var state = response.getState();
                               if (state === "SUCCESS") 
                                   var contactid=response.getReturnValue().Id;
                                   alert("contact created.Id:"+contactid)
                                   var toastEvent = $A.get("e.force:showToast");
                                       "title": "Success!",
                                       "message": "Contact record Created successfully.",
                                   var navEvt = $A.get("e.force:navigateToSObject");
                                       "recordId": contactid,
                                       "slideDevName": "related"

<!------ Apex-->
public class createrelatedcontact 
    public static contact insertCon(Contact newcon)
        insert newcon;
        return newcon;        
    public static Account getAccount(string acid)
        Account acc = [select id,name from Account where Id =:acid];
        return acc;
Hello !
Does any one resolve this use case ?
Thanks !