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
Saurabh Bisht 19Saurabh Bisht 19 

create a contact record with custom lookup field in salesforce lwc

please code it in LWC with html,css,js and apex class looklup field is Account in Contact
vikas singhvikas singh
Hi  Saurabh Bisht 19
please put your full requirement what exactly you want after that may be I can help you   
  thank you 
Saurabh Bisht 19Saurabh Bisht 19
we want to create contact record from lwc using forms and mns we have tgo create a components and deploy it to salesforce org whenever we fill that form record of contact created and one field should be there is Account field
Saurabh Bisht 19Saurabh Bisht 19
first name
lastname
Account
these 3 field i need in my components form
vikas singhvikas singh
Hi  Saurabh Bisht 19
-------------------------------------------------Html file ----------------------------------------------------------------------

<template>
      <div>
        <div class="slds-p-around_medium lgc-bg">
            <lightning-input type="text" label="first name" onchange={firstNames}></lightning-input>
        </div>
        <div class="slds-p-around_medium lgc-bg">
            <lightning-input type="text" label="lastname"  onchange={lastNames}></lightning-input>
        </div>
        <div class="slds-p-around_medium lgc-bg">
            <lightning-combobox
            name="progress"
            label="Account"
            value={value}
            placeholder="Select Progress"
            options={options}
            onchange={handleChange} ></lightning-combobox>
        </div>
        <lightning-button label="submit" onclick={createContact}> </lightning-button>
      </div>
</template>


 ----------------------------------------------------------------------------------------js File-------------------------------------------------------------------------------

import { LightningElement } from 'lwc';
import getallAccount from '@salesforce/apex/CreateContact.allAccount'
import createContact from '@salesforce/apex/CreateContact.createContact'
export default class Createcontact extends LightningElement {
    account = '';
    firstName = '';
    lastName = ''
    options = []
    constructor() {
        super();
        this.getAccountInfo();
    }
    getAccountInfo() {
        getallAccount().then(item => {
            this.options = item.map(items => {
                return {
                    label: items.Name,
                    value: items.Id
                }
            })
        })
    }
    handleChange(event) {
        this.account = event.detail.value;
    }
    firstNames(event) {
        this.firstName = event.detail.value;
        console.log(this.firstName);
    }
    lastNames(event) {
        this.lastName = event.detail.value;
        console.log(this.lastName);
    }

    createContact() {
        console.log(this.lastName, this.firstName, this.account);
        createContact({ "Fname": this.firstName, "Lname": this.lastName, "accountId": this.account }).then(item => {
            console.log("value insrted");
        })
    }
}
--------------------------------------------------- Apex Controller -----------------------------------------------------------------------
public  without sharing class CreateContact {
    @AuraEnabled(cacheable=false)
    public static list<Account> allAccount(){
            list<account> accountList =[SELECT Id,Name from Account]; 
            system.debug('accountList:::'+accountList);
           return accountList;
    }
      @AuraEnabled(cacheable=false)
    public static void createContact(string Fname, string Lname, string accountId){
        try{
              system.debug('Fname'+Fname);
            system.debug('Lname'+Lname);
            system.debug('Fname'+Fname);
            if(Fname != null && Lname != null && accountId != null){
                contact cont = new contact();
                cont.LastName =Lname;
                cont.AccountId = accountId;
                cont.FirstName =Fname;
                insert cont; 
            }
        
        }catch(Exception ex){
             system.debug('Exception::'+ex.getMessage()+'at'+ex.getLineNumber());
            
        }
    }
}

 if it is helpful for you please mark it as the best answer
 thank you