+ Start a Discussion
Athira VenugopalAthira Venugopal 

Unable to set values on lightning-record-view-form

    <lightning-card title="Custom Search Functionality in LWC" icon-name="standard:account">
        <div if:true={errorMsg} style="margin-left: 3%;">
            <p style="color: red;">{errorMsg}</p>
        <lightning-layout multiple-rows="true" vertical-align="end">
            <lightning-layout-item size="12" small-device-size="10" medium-device-size="8" large-device-size="6" padding="around-small">
                    <div class="slds-form-element">
                            <div class="slds-form-element__control">
                                    <lightning-input type="text" 
                                                     label="Enter Customer Name" name ="cusName"
                                                     onchange={handleCustomerName} ></lightning-input>
            <lightning-layout-item size="12" small-device-size="2" medium-device-size="2" large-device-size="2" padding="around-small">
                    <lightning-button label="Search" 
            <template if:true={show}>
                <div class="acc-container">
                    <lightning-record-view-form record-id={recordId} object-api-name="Client__c">
                        <div class="slds-grid">
                            <div class="slds-col slds-size_1-of-2">
                                <lightning-output-field field-name="Name"></lightning-output-field>
                                <lightning-output-field field-name="Date_of_Birth__c"></lightning-output-field>
                            <div class="slds-col slds-size_1-of-2">
                                <lightning-output-field field-name="Photo__c"></lightning-output-field>
                                <lightning-output-field field-name="Booking_Date__c"></lightning-output-field>


import { LightningElement, track } from 'lwc';
import getDetail from '@salesforce/apex/PriceFetch.getDetail';
export default class ClientDefinition extends LightningElement {
    @track show = false;
    @track recordId;
    handleCustomerName(event) {
        this.cusName =  event.target.value;
    handleSearch() {
       /* if(this.cusName.equals('')) {
            this.errorMsg = 'Please enter customer name to search.';
        getDetail({ clientName: this.cusName})
           .then(result => {
            this.show = true;
            this.recordId = result;
           .catch(error => {
                this.error = error;
                alert("FAILURE" + error);
                 //this.error = error;

Apex controllr class

public with sharing class PriceFetch {
    public static String getDetail(String clientName) {
        String search = '%' + clientName + '%';
       List<Client__c> clients = new List<Client__c>();
         clients = [
            SELECT Id, Name, Unit__c, Total_cost__c, Remarks__c, Project__c,Profession__r.Name,Photo__c,PAN__c,Father_Spouse__c,Email_id__c,
            FROM Client__c
            WHERE Name LIKE :search
            LIMIT 1];
            if(clients.size() == 0) {
                 throw new AuraHandledException('exceptionText');
           return clients.get(0).Id;


Is there any mistake in my code?
Im not getting the recordId.
Best Answer chosen by Athira Venugopal
Maharajan CMaharajan C
Hi Athira,

I have tried your code except the soql query and am able to get the record id also record edit form populating the values.

1. Narrow down the query. Remove the unused fields from SOQL.
2. Put the system.debug statements in Apex class.  Check the debug log or logs in dev console.
3. Are you getting the javascript alert? am getting the alert with Id.
4. Check the Field level security for the fields reffered in Record-Edit-Form. Also check object and record level access also.

Below is my Apex code:
    public static String getDetail(String clientName) {
        String search = '%' + clientName + '%';
       		List<Account> clients = [Select Id, Name ,industry, AccountNumber from Account
           							WHERE Name LIKE :search
            						LIMIT 1];
            if(clients.size() == 0) {
                 throw new AuraHandledException('exceptionText');
           return clients.get(0).Id;


User-added image