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
kallam salesforce1kallam salesforce1 

Unable to fetch results, Can someone help what is the issue in this program?

public with sharing class lDS_GetContacts_Wire2 {
    @AuraEnabled(cacheable=true)
    public static list<Contact> findContacts(string searchtext){
          string key = '%'+searchtext+'%';
          return [Select id,Name,Phone,Email from Contact where FirstName LIKE:key];
    }
}

HTML FILE

<template>
    <lightning-card title="Search Contact WIRE">
        <lightning-input label="Enter the Search Text" type="search" value={searchkey} onchange={handleOnchange}>
            <template if:true={contact.data}>
                <template for:each={contact.data} for:item="con">
                    <p key={con.id}>{con.Name}</p>
                </template>
            </template>
            <template if:true={contact.error}>
                Sorry we can't display your record due to {contact.error}. Please Try Later.
            </template>
        </lightning-input>
    </lightning-card>
</template>

Js File >>>>>>>
import { LightningElement,track,wire} from 'lwc';
import findcontacts from '@salesforce/apex/lDS_GetContacts_Wire2.findContacts'
export default class LDS_SearchContact_Wire extends LightningElement {
    @track searchkey;
    @wire (findcontacts,{searchtext :'$searchkey'}) contact;
    handleOnchange(event){
        this.searchkey = event.target.value;
    }
}



 
CharuDuttCharuDutt
Hii kallam salesforce1
Try Below Code
<lightning-card title="Search Contact WIRE">
        <div class="slds-m-around_medium">
          <lightning-input label="Enter the Search Text" type="search" value={searchkey} onchange={handleOnchange}></lightning-input>
              <ul class="slds-m-around_medium">
            <template for:each={contacts} for:item="contact">
                <li key={contact.Id}>
                    {contact.Name}
                </li>
            </template>
        </ul>
           
        </div>
    </lightning-card>


#####################################################################

import { LightningElement,api,track,wire } from 'lwc';
import findcontacts from '@salesforce/apex/lDS_GetContacts_Wire2.findContacts';
export default class LDS_SearchContact_Wire extends LightningElement {
    @track contacts;
    @track searchkey;

    @wire(findcontacts,{
        searchtext : '$searchkey'
    })
    wireddata({ error, data }){
    if (data) 
    {   var ObjData = JSON.parse(JSON.stringify(data));
        this.contacts = ObjData;
        }
    else if (error) {
            this.error = error;
            this.contacts = undefined;
        }
    }
    handleOnchange(event){
        this.searchkey = event.target.value;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
kallam salesforce1kallam salesforce1

Hi CharuDutt,

Your code is woring fine but what is the error in my code.