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
MRITYUNJAY PATEL 3MRITYUNJAY PATEL 3 

How to insert exiting accounts in contact with aura component

Hi Everyone,
i am trying to create form of contact with display the exitsting account in dropdown menu and after select create a new contact with exitsting account name.
 
Best Answer chosen by MRITYUNJAY PATEL 3
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code, it will help to insert contact with a lookup of account:-
// aura cmp

<aura:component controller="AuraApex" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="AccountList" type="List" />
    <div>
        <lightning:select name="select1" label="List Of Account"  aura:id="AccountId"   required="false" >
            
            <aura:iteration items="{!v.AccountList}" var="ac" >
                <option value="{!ac.Id}" label="{!ac.Name}" />
            </aura:iteration>
        </lightning:select>
    </div>
    
    <aura:attribute name="LastName" type="String"  />
    <div>
        <lightning:input type="String" label="LastName" aura:id="lastn" value="{!v.LastName}" />
        <lightning:button variant="brand" label="Insert" title="Brand action" onclick="{! c.handleClick }" />    
    </div>
</aura:component>


/////// controller.js
({
	doInit : function(component, event, helper) {
		helper.helperMethod(component, event, helper);
	},
    
    handleClick: function(component, event, helper){
    helper.createContact(component, event, helper);
}
})

////////// helper.js

({
    helperMethod : function(component, event, helper) {
		var action=component.get("c.getAccount");
        action.setCallback(this,function(response){
            console.log(response.getReturnValue());
            if(response.getReturnValue()!=null){
                component.set("v.AccountList",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
    createContact: function(component, event, helper) {
     var acid=component.find('AccountId').get('v.value');
     var lname=component.find('lastn').get('v.value');
        var action=component.get("c.AddContact");
      console.log(acid);
         console.log(action);
		 action.setParams({
            id1:acid,
            ln:lname          
        });
        action.setCallback(this,function(response){
           
            if(response.getReturnValue()!=null){
                console.log("inside callback");
                 console.log(JSON.stringify(response.getReturnValue()));
                alert("contact is inserted");
            }
        });
        $A.enqueueAction(action);
        
    }
})

//////////////   APEX

public with sharing class AuraApex {
   @AuraEnabled(cacheable = true)
    public static list<account> getAccount(){
        List<account> AccList = new list<account>();
        AccList = [select Id,Name from Account limit 100];
        system.debug('ac'+acclist);
        return AccList;
    }
    
    @auraenabled
    public static contact AddContact(id id1, string ln){
        system.debug('con apex'+id1+','+ln);
        contact c =new contact();
        c.AccountId = id1;
        c.LastName = ln;
        c.User_Name__c='ahsha';
		c.Password__c='qwqw';
        insert c;
        return c;
    }
}
Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
    

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Mrityunjay,

>> https://salesforce.stackexchange.com/questions/47699/how-to-auto-populate-account-name

The above link has a thread for a way to populate the account name for a new contact record.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code, it will help to insert contact with a lookup of account:-
// aura cmp

<aura:component controller="AuraApex" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="AccountList" type="List" />
    <div>
        <lightning:select name="select1" label="List Of Account"  aura:id="AccountId"   required="false" >
            
            <aura:iteration items="{!v.AccountList}" var="ac" >
                <option value="{!ac.Id}" label="{!ac.Name}" />
            </aura:iteration>
        </lightning:select>
    </div>
    
    <aura:attribute name="LastName" type="String"  />
    <div>
        <lightning:input type="String" label="LastName" aura:id="lastn" value="{!v.LastName}" />
        <lightning:button variant="brand" label="Insert" title="Brand action" onclick="{! c.handleClick }" />    
    </div>
</aura:component>


/////// controller.js
({
	doInit : function(component, event, helper) {
		helper.helperMethod(component, event, helper);
	},
    
    handleClick: function(component, event, helper){
    helper.createContact(component, event, helper);
}
})

////////// helper.js

({
    helperMethod : function(component, event, helper) {
		var action=component.get("c.getAccount");
        action.setCallback(this,function(response){
            console.log(response.getReturnValue());
            if(response.getReturnValue()!=null){
                component.set("v.AccountList",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
    createContact: function(component, event, helper) {
     var acid=component.find('AccountId').get('v.value');
     var lname=component.find('lastn').get('v.value');
        var action=component.get("c.AddContact");
      console.log(acid);
         console.log(action);
		 action.setParams({
            id1:acid,
            ln:lname          
        });
        action.setCallback(this,function(response){
           
            if(response.getReturnValue()!=null){
                console.log("inside callback");
                 console.log(JSON.stringify(response.getReturnValue()));
                alert("contact is inserted");
            }
        });
        $A.enqueueAction(action);
        
    }
})

//////////////   APEX

public with sharing class AuraApex {
   @AuraEnabled(cacheable = true)
    public static list<account> getAccount(){
        List<account> AccList = new list<account>();
        AccList = [select Id,Name from Account limit 100];
        system.debug('ac'+acclist);
        return AccList;
    }
    
    @auraenabled
    public static contact AddContact(id id1, string ln){
        system.debug('con apex'+id1+','+ln);
        contact c =new contact();
        c.AccountId = id1;
        c.LastName = ln;
        c.User_Name__c='ahsha';
		c.Password__c='qwqw';
        insert c;
        return c;
    }
}
Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
    

 
This was selected as the best answer