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
RazaRaza 

How to make component that will use to create a new contact with account lookup.(using component)

Hi 
please help me
How to make component that will use to create a new contact with account lookup.(using component)
 
Ajay K DubediAjay K Dubedi

Hi Micle0195,

For Creating a  component that will use to create a new contact with Lookup please try the below code:

//Apex Controller 
 
public class contactSaveCtrl {
 @AuraEnabled 
    public static void saveContact(Contact con){
        insert con;
    }   
}
 
//Lightning Component 

<aura:component controller="contactSaveCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    <aura:attribute name="selectedLookUpRecord" type="sObject" default="{}"/>
    <aura:attribute name="objContact" type="contact" default="{'sobjectType':'contact'}"/>
  
  <div class="slds-m-around_large">
      <ui:inputText class="slds-input" value="{!v.objContact.LastName}" label="Last Name"/>
 
      <c:customLookup objectAPIName="account" IconName="standard:account" label="Account Name" selectedRecord="{!v.selectedLookUpRecord}"/>
      
<br/> 
      
    <button class="slds-utton slds-button_brand" onclick="{!c.saveContactRecord}">Save Contact</button>    
  </div>       
</aura:component>

//JavaScript Controller 

({
 saveContactRecord : function(component, event, helper) {
        var conObj = component.get("v.objContact");
           //set the default accountId is null 
           conObj.AccountId = null ; 
       // check if selectedLookupRecord is not equal to undefined then set the accountId from 
       // selected Lookup Object to Contact Object before passing this to Server side method
        if(component.get("v.selectedLookUpRecord").Id != undefined){
          conObj.AccountId = component.get("v.selectedLookUpRecord").Id;
        } 
        
       //call apex class method
      var action = component.get('c.saveContact');
        action.setParams({
            'con': conObj
        })
      action.setCallback(this, function(response) {
        //store state of response
        var state = response.getState();
        if (state === "SUCCESS") {
         alert('Record Created');
        }
      });
      $A.enqueueAction(action);
        
       
 }
})

If you found this answer helpful then please mark it as best answer so it can help others.

Thanks 
Ajay Dubedi