Created A component to Create a New Contact And Added it to Account Page layout. now i want to Connect That new contact to Account in which we have.


public class CreateContact_Ctrl {
    public static void getContact(string serverLastName,string serverEmail,string serverPhone){
        Contact con = new Contact();
        system.debug('value of lastName ==>' + serverLastName);
        system.debug('value of last Email ==>'+ serverEmail);
        system.debug('value of last Phone ==>'+ serverPhone);
        con.LastName = serverLastName ;
        con.Email = serverEmail ;
        con.Phone = serverPhone ;
        con.Department = 'Created Via Component';
         insert con;

Aura Component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" 
                controller="CreateContact_Ctrl" access="global" >
    <aura:attribute name="lastname" type = "String"/>
    <aura:attribute name="Email" type = "String"/>
    <aura:attribute name="Phone" type = "String"/>
     <lightning:card footer="Click Save button to Create New Record" title="Create Contact">
        <aura:set attribute="actions">
            <lightning:button label="save" onclick="{!c.createContact}"/>
        <p class="slds-p-horizontal_small">            
            <lightning:input name="input3" label="Enter LastName" placeholder="type here..." value="{!v.lastname}"/>
            <lightning:input name="input3" label="Enter Email" placeholder="type here..." value="{!v.Email}"/>
            <lightning:input name="input3" label="Enter Phone" placeholder="type here..." value="{!v.Phone}"/>

Aura Controller

    createContact : function(component, event, helper) {
        //console.log('method Called');
        var clientLastName = component.get("v.lastname");
        var clientEmail = component.get("v.Email");
        var clientPhone = component.get("v.Phone");
        //var clientAccount = component.get("v.Account");
        //call Apex Class Method
        var action = component.get("c.getContact");
        //Pass parameter to apex Method (First Time).....
            //Placing the request to server to run it....
            var state = respons.getState();
            if(state=== "SUCCESS"){
            var toastEvent = $A.get("e.force:showToast");
            title: 'info',
            message:'record Created Successfully !!!',
  • can you narrow down what error you are getting? your questions is very broad and asking just how to develop something. – anxiousAvocado May 17 at 2:13
  • i want to add new Contact (which created by component) to Add in existing Account – mirza Kahekashan May 17 at 9:38

So you need to add the Account link to the new Contact. To do that, use recordId param you have implicitly on your Aura Component

Aura component: add this near other aura:attribute rows

    <aura:attribute name="recordId" type = "String"/>

It will be prefilled automatically with Account Id Then send this Id to the server (in JS controller):


Then use it in Apex

    public static void getContact(string serverLastName,string serverEmail,string serverPhone, Id serverAccountId){
         // ...  leave the beginning of the method as is
        con.accountId =  serverAccountId;           
        insert con;
  • Record Created As before but not Added To Existing Account – mirza Kahekashan May 17 at 9:54
  • @mirzaKahekashan, can you add the console.log to see what goes into the params and check if the account Id is filled? – Jack Veromeev Jun 2 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.