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
Sukriti SharmaSukriti Sharma 

How to take input from user in LWC and pass parameter from js to apex class

I am trying to have a LWC card on home screen with an input field of name and a button. when clicked it should create the account by the same name entered b user. I have been trying to pass the parameter to Apex Class but it keeps giving an error.
Here's my code :
addAccount.html 
<template>
    <lightning-card  title="Create your Account">
        <p class="slds-p-horizontal_small">
            <lightning-record-edit-form 
                object-api-name="Account" 
                onsuccess={handleSuccess}>
                <lightning-input-field field-name="Name" name="name"> 
                </lightning-input-field>            
            </lightning-record-edit-form>
        </p>
        <p slot="footer">
            <lightning-button
                type="submit"
                name="submit"
                label="Create Account"
                onclick={handleClick}>
            </lightning-button>
        </p>
    </lightning-card>
</template>
addAccount.js
import { LightningElement, track, api, wire } from 'lwc';
import insertNewAccount from '@salesforce/apex/AddAccount.insertNewAccount';
export default class AddAccount extends LightningElement {
    @track Name;
    handleClick(event)
    {
        console.log(event.target.label);
        var inp=this.template.querySelector("lightning-input-field");
        this.Name=inp.value;
        console.log(inp.value);
        insertNewAccount("accountName:this.Name").then(result=>{
            console.log(JSON.stringify(result));
        }).catch(error=>{
            console.log(error);
        })
    } 
}
AddAccount.cls
public with sharing class AddAccount {
    @AuraEnabled
    public static Account insertNewAccount(String accountName){
        Account newAccount = new Account();
        newAccount.Name = accountName;
        insert newAccount;
        return newAccount;
    }
}
Please help with passing the paramenterThe warnings in consoleIt doesn't create an account
 
Best Answer chosen by Sukriti Sharma
mukesh guptamukesh gupta
Hi Sukriti,

Please use below code:-

addAccount.js​​​​​​​
import { LightningElement, track, api, wire } from 'lwc';
import insertNewAccount from '@salesforce/apex/AddAccount.insertNewAccount';
export default class AddAccount extends LightningElement {
    @track Name;
    handleClick(event)
    {
        console.log(event.target.label);
        var inp=this.template.querySelector("lightning-input-field");
        this.Name=inp.value;
        console.log(inp.value);
        insertNewAccount({accountName:this.Name}).then(result=>{
            console.log(JSON.stringify(result));
        }).catch(error=>{
            console.log(error);
        })
    } 
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Sukriti,

Refer the below link have solution for similar kind of ask and modify your code.
https://www.w3web.net/insert-a-record-of-account-lightning-web-component/

If this information helps, please mark it as best answer.

thanks!!
mukesh guptamukesh gupta
Hi Sukriti,

Please use below code:-

addAccount.js​​​​​​​
import { LightningElement, track, api, wire } from 'lwc';
import insertNewAccount from '@salesforce/apex/AddAccount.insertNewAccount';
export default class AddAccount extends LightningElement {
    @track Name;
    handleClick(event)
    {
        console.log(event.target.label);
        var inp=this.template.querySelector("lightning-input-field");
        this.Name=inp.value;
        console.log(inp.value);
        insertNewAccount({accountName:this.Name}).then(result=>{
            console.log(JSON.stringify(result));
        }).catch(error=>{
            console.log(error);
        })
    } 
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
This was selected as the best answer