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
Piyush DeveloperPiyush Developer 

How to pass parameter in apex method using connected call back method in LWC

SwethaSwetha (Salesforce Developers) 
HI Piyush,
We can call an apex method inside the connectedCallback() in Lightning Web Component.
Example:
//lifeCycle.html
<template>
    <template if:true={contacts}>
        <template for:each={contacts} for:item="contact">
            <p key={contact.Id}>{contact.Name}</p>
        </template>
    </template>
</template>
 
//lifeCycle.js
import { LightningElement, track } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';

export default class LifeCycle extends LightningElement {
    @track contacts;
    @track error;

   connectedCallback(){
        getContactList()
        .then(result => {
            this.contacts = result;
            this.error = undefined;
        })
        .catch(error => {
            this.error = error;
            this.contacts = undefined;
        });
   }    
}

See related:https://salesforcediaries.com/2019/12/13/connectedcallback-in-lightning-web-component/
https://www.codekiat.com/2020/04/how-to-pass-record-id-from-lightning-web-component-to-apex-controller.html

If this information helps, please mark the answer as best. Thank you
Suraj Tripathi 47Suraj Tripathi 47
Hi Piyush,
You can take references from below code.
----Apex class
public class InsertContact_Controller14 {
    
    @AuraEnabled
    public static boolean contactInsert(string conObj ,string accountId){
        try{
            
            contact con = new contact();
            con.LastName=conObj;
            con.AccountId=accountId;
            
            insert con;
            system.debug('con-->'+con);
            return true;
        }
        catch(Exception e){
            system.debug('Message-->'+e.getMessage());
            system.debug('Line No-->'+e.getLineNumber());
            
            return false;
        }
    }
    @AuraEnabled
    public static List<Account> AccountFetch(){
        
        system.debug('Account Find');
        List<Account> accList = [SELECT id ,Name FROM Account limit 100];
        return accList ;
    }  
}
-------html
<template>
    <lightning-card title="Create Account With Contact" icon-name="standard:contact">
      
                <div class="form-group">
                    <lightning-input label="Enter Last Name" name="accName" required="required" type="text"  onchange={handleNameChange}></lightning-input>
                </div>
                <div class="slds-m-around_medium">
                    <select class="slds-select" onchange={AccountIdFetch}>
                    <option>Select Account</option>
                    <template for:each ={acc} for:item="item">
                    <option key={item.Id} value={item.Id}>{item.Name}</option>
                    </template>
                    </select>
                    </div>
                
            <div>
                    <lightning-button label="submit" onclick={forSubmit}></lightning-button>
                </div>
             
    </lightning-card>
    
</template>
------.js
import { LightningElement, track } from 'lwc';
import fetchData from '@salesforce/apex/InsertContact_Controller14.contactInsert';
import fetchAccount from'@salesforce/apex/InsertContact_Controller14.AccountFetch';
export default class Question8 extends LightningElement {
@track acc;
@track lName;
@track accId;
 message;
handleNameChange(event) {
    this.lName=event.target.value;
    console.log('Name',this.lName);

}
    connectedCallback(){
        fetchAccount()
        .then(result => {
        this.acc = result;
        
        console.log(JSON.stringify(result));
        console.log("result",this.acc);
        })
       
    }    
    AccountIdFetch(event){
        this.accId=event.target.value;
        console.log('accId',accId);

    }

    forSubmit(event){
        fetchData({conObj :this.lName ,accountId : this.accId })
        .then(result => {
        this.message = result;
        
        console.log(JSON.stringify(result));
        console.log("result",this.message);
        })
        .catch(error => {
            this.error = error.message;
        });
    }
    
}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi