Skip to main content
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 paramenter

How to take input from user in LWC and pass parameter from js to apex classIt doesn't create an account

 
답변 2개
  1. 2022년 6월 15일 오전 9:43
    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

     
0/9000