+ Start a Discussion
Athira VenugopalAthira Venugopal 

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

HTML TEMPLATE
<template>
    <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>
        </div>
        <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>
                            </div>
                        </div> 
            </lightning-layout-item>
            <lightning-layout-item size="12" small-device-size="2" medium-device-size="2" large-device-size="2" padding="around-small">
                    <lightning-button label="Search" 
                                      variant="brand" 
                                      onclick={handleSearch}></lightning-button>
                </lightning-layout-item>
            </lightning-layout><br/>
            <template if:true={show}>
                <div class="acc-container">
                    {recordId}
                    <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>
                            <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>
                            </div>
                        </div>
                    </lightning-record-view-form>
                </div>
            </template>
           
      
    </lightning-card>
</template>

js

import { LightningElement, track } from 'lwc';
import getDetail from '@salesforce/apex/PriceFetch.getDetail';
export default class ClientDefinition extends LightningElement {
    cusName;
    @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.';
          
            return;
        }*/
        alert(this.cusName);
     
        getDetail({ clientName: this.cusName})
          
        
           .then(result => {
            this.show = true;
            alert(result);
            this.recordId = result;
           })
        
           .catch(error => {
                this.error = error;
                alert("FAILURE" + error);
                 //this.error = error;
           });
            
    }
}

Apex controllr class

public with sharing class PriceFetch {
  
    @AuraEnabled
    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,
            Date_of_Birth__c,Contact_Number__c,Communication_address__c,Booking_Date__c,Booking_Amount__c,Agreement_Date__c,Aadhar_Number__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:
 
@AuraEnabled
    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;
        
    }

Output:

User-added image

Thanks,
Maharajan.C