You need to sign in to do that
Don't have an account?
Saurav Roy 15
Imperative Method LWC
Hi All,
Could you please look into the code and let me know where i am wrong.
I am trying the retrieve the list of accounts using imperative method. The data is not retrieving and it the going to the catch error block.
Thanks
Saurav
Could you please look into the code and let me know where i am wrong.
I am trying the retrieve the list of accounts using imperative method. The data is not retrieving and it the going to the catch error block.
Apex Class public with sharing class ContactListService { @AuraEnabled public static List<Object> getAcountList(String name){ String key = '%'+name+'%'; String accQuery = 'select id, name, email, phone from Account where name like : key'; System.debug('Query --- '+Database.query(accQuery)); return Database.query(accQuery); } } JS File: import { LightningElement, track } from 'lwc'; import getAcountList from '@salesforce/apex/ContactListService.getAcountList'; export default class ContactList extends LightningElement { @track accounts; @track errorAccount; @track searchAccount; handleChangeAccount(event){ //event.preventDefault(); console.log('Value '+event.target.value); this.searchAccount = event.target.value; } //Imperative method findaccounts(){ getAcountList({ name : this.searchAccount }) .then(result =>{ console.log('Result----'+result); this.accounts = result; }) .catch(error=>{ this.errorAccount = error; console.log('Error ----'+error); }); } } HTML: <template> <lightning-card> <lightning-layout vertical-align="center"> <lightning-layout-item padding="around-small"> <lightning-input label="Find Account" value={searchAccount} onchange={handleChangeAccount}></lightning-input> </lightning-layout-item> <lightning-layout-item padding="around-small"> <lightning-button label="Find Accounts" varient="brand" onclick={findaccounts}></lightning-button> </lightning-layout-item> <lightning-layout-item padding="around-small"> <template for:each={accounts} for:item="acc"> <p key={acc.id}>{acc.Name}</p> </template> </lightning-layout-item> </lightning-layout> </lightning-card> </template>
Thanks
Saurav
1. In account object we don't have standard field Email. So remove email field from apex query.
2. In Lightning Button change spalling for varient="brand" => variant="brand"
3. use the if:true Accounts check in template before the for each.
4. i have updated your template code to fix the alignments.
5. use the acc.Id in Key for <p>. here letter(i) should be in caps.
Updated Code Below:
Apex Class:
HTML File:
JS File: No update in JS
XML File:
Thanks,
Maharajan.C
All Answers
1. In account object we don't have standard field Email. So remove email field from apex query.
2. In Lightning Button change spalling for varient="brand" => variant="brand"
3. use the if:true Accounts check in template before the for each.
4. i have updated your template code to fix the alignments.
5. use the acc.Id in Key for <p>. here letter(i) should be in caps.
Updated Code Below:
Apex Class:
HTML File:
JS File: No update in JS
XML File:
Thanks,
Maharajan.C