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.
<!--Component-->
<aura:component controller="CreateRelatedcontact" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,
                            flexipage:availableForRecordHome,
                            force:hasRecordId,forceCommunity:availableForAllPageTypes,
                            force:lightningQuickAction" 
                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> 
    <div class="slds-form-element__control">
        FirstName  <force:inputField value="{!v.contact.FirstName}"/>
    </div>
    <div class="slds-form-element__control">
        LastName  <force:inputField value="{!v.contact.LastName}"/>
    </div>            
    <lightning:button aura:id="insert" label="Create Contact" class="slds-button slds-button--brand buttontype" onclick="{!c.InsertCont}" />  
    
</aura:component>

<!-- Controller-->
({    
    doInit : function(component, event, helper) {
        var accid = component.get("v.recordId");
        component.set("v.contact.AccountId",component.get("v.recordId"));
        var action=component.get("c.getAccount");
        action.setParams({
            acid : component.get("v.recordId")            
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS"){
                component.set("v.account",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
        
    },
    
    InsertCont : function(component){
        
        var action=component.get("c.insertCon");
        action.setParams({ "newcon": component.get("v.contact") });
        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");
                                   toastEvent.setParams({
                                       "title": "Success!",
                                       "message": "Contact record Created successfully.",
                                       "mode":"pester"
                                   });
                                   toastEvent.fire();
                                   
                                   var navEvt = $A.get("e.force:navigateToSObject");
                                   navEvt.setParams({
                                       "recordId": contactid,
                                       "slideDevName": "related"
                                   });
                                   navEvt.fire();
                               }
                           });
        $A.enqueueAction(action);
    }
})

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