0

I am facing an error in the wire method of this code

JS

import { LightningElement,api,wire} from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import  Date_of_Visit__c from '@salesforce/schema/Site_Visit__c.Date_of_Visit__c';
import Flags__c from '@salesforce/schema/Site_Visit__c.Flags__c';
import Details__c from '@salesforce/schema/Site_Visit__c.Details__c';
import GrantName from '@salesforce/schema/Site_Visit__c.Grant__c';
import getSingleSitevisit from '@salesforce/apex/Sitevisit.getSingleSitevisit';

export default class Lwcdata extends LightningElement {
    @api recordId;
    @api objectApiName;
    @api recordtypeId;
    date=Date_of_Visit__c;
    details=Details__c;
    picklist=Flags__c;
    
    Grant=GrantName;        
    areDetailsVisible = true;   

    handleSuccess(){
        if(this.recordId !== null){
            this.dispatchEvent(new ShowToastEvent({
                title: "SUCCESS!",
                message: "Record has been Updated.",
                variant: "success",
            }),  
            );
        }

        //this.areDetailsVisible = false;
    }
     
    handleEdit(event){
        this.areDetailsVisible = true;
        console.log('visibleedit@@==>'+this.areDetailsVisible);
    }       

    @wire(getSingleSitevisit,{recordId: '$recordId' })
    wiredRecord({ error, data }) {
        if (error) {
            console.log('error occured',error);
        } else if (data) {
            console.log('recordId==>'+recordId);
        }
    }
}

HTML:

<template>   
    <template if:true={areDetailsVisible}>
        <lightning-card title="Site Visits during the Post Award Phase" icon-name="utility:add data">
            <div class ="slds-p-bottom_large slds-p-left-large" style="width:500 px">
                <lightning-record-edit-form
                    object-api-name="Site_Visit__c"
                    onsuccess={handleSuccess}>
                        
                        <lightning-messages></lightning-messages>
                        
                        <lightning-input-field field-name={date}></lightning-input-field> 
                        <lightning-input-field field-name={details}></lightning-input-field>
                        <lightning-input-field field-name={picklist}> </lightning-input-field>        
                        <lightning-input-field field-name={Grant} value={recordId} class="slds-hidden"> </lightning-input-field>   
                        <lightning-button type="submit" name="save" label="save" class="slds-m-top_medium"  ></lightning-button>            
                </lightning-record-edit-form>
            </div>
        </lightning-card>
    </template>   

    <!--<template if:false={areDetailsVisible}>
        <lightning-card title="Site Visits during the Post Award Phase" icon-name="utility:add data">
            <div class ="slds-p-bottom_large slds-p-left-large" style="width:500 px">
                <lightning-record-edit-form
                    id="recordViewForm1"
                    record-id={recordId}
                    record-type-id={recordtypeId}
                    object-api-name={objectApiName}>

                        <lightning-messages></lightning-messages>
                        
                        <lightning-output-field field-name="Flagstatus__c" variant="label-hidden"> </lightning-output-field>   
                        <lightning-output-field field-name="Date_of_Visit__c"> </lightning-output-field>
                        <lightning-output-field field-name="Details__c"></lightning-output-field>                           
                        <lightning-button type="edit" name="edit" label="edit" class="slds-m-top_medium" onclick={handleEdit}></lightning-button>            
                 </lightning-record-edit-form>
           </div>
       </lightning-card>

    </template>-->
</template>

APEX Class:

public with sharing class Sitevisit{
    @AuraEnabled(cacheable=true)
    public static Site_Visit__c getSingleSitevisit(string recordId) {
        return [
            SELECT Id, Date_of_Visit__c,Flags__c,Details__c,Grant__c
            FROM Site_Visit__c
            WHERE Grant__c=:recordId
            ORDER BY LastModifiedDate DESC
            LIMIT 1
        ];
    }
}

Here initially my component was used to store data in the opportunity object. Each time I entered data in the opportunity it gets recorded in the related field of the opportunity. Right now I have added a lookup field named site and these details I have added should get recorded in site object as well. I have almost completed and while debugging the apex class works. But while trying to pass the recordId in the wire an error is shown RecordId not found while checking the components front end. Why is it so?

5
  • console.log('recordId==>'+recordId); should be console.log('recordId==>', this.recordId);. Without this it is a local variable, but that function has no recordId attribute.
    – RubenDG
    Feb 23, 2022 at 12:33
  • @RubenDG Yes you are right and I have changed the values in the console.log.The error is solved but still the values aren't getting entered there.
    – user110290
    Feb 23, 2022 at 14:11
  • Currently you do nothing with the record returned by the wired method, if any. Moreover it's called asap, while if I understood the requirement you would like to get the record created by the record-edit-form, is it right?
    – RubenDG
    Feb 23, 2022 at 14:43
  • @RubenDG Yes exactly that is the requirement
    – user110290
    Feb 23, 2022 at 14:54
  • @RubenDG But I am returning the values I require from the apex class.So I wont' be needing to populate the fields in the wire method right?
    – user110290
    Feb 23, 2022 at 15:45

1 Answer 1

1

You can get the id of the record created by the record-edit-form in the handleSuccess function:

createdRecordId;

handleSuccess(event) {
    // this.recordId is always != null
    this.dispatchEvent(new ShowToastEvent({
        title: "SUCCESS!",
        message: "Record has been Updated.",
        variant: "success",
    }) 
    );
    this.createdRecordId = event.detail.id;
}

Now you have to choose between using an Apex method or rely on LDS. I would prefer the latter.

LDS

In order to get the record you don't need the Apex method. You could use getRecord adding the following import

import { getRecord } from 'lightning/uiRecordApi';

then you can wire it:

@wire(getRecord, { recordId: '$createdRecordId', fields: [Date_of_Visit__c, Flags__c, Details__c, GrantName] })
wiredRecord;

Since wired properties are reactive, as soon as you set createdRecordId, the getRecord method will be invoked.
Finally you could use getFieldValue to get field's value from the wired record.

Apex

If you choose to stick with Apex, you can change the method as follow:

@AuraEnabled(cacheable=true)
public static Site_Visit__c getSingleSitevisit(Id recordId) {
    return [
        SELECT Id, Date_of_Visit__c, Flags__c, Details__c, Grant__c
        FROM Site_Visit__c
        WHERE Id = :recordId
        LIMIT 1
    ];
}

Then change the wired function:

wiredRecord;

@wire(getSingleSitevisit,{recordId: '$createdRecordId' })
fetchSingleVisit({ error, data }) {
    if (error) {
        console.log('error occured', error);
    } else if (data) {
        console.log('data==>', data);
        this.wiredRecord = data;
    }
}

One way or another wiredRecord will hold the required data.

1
  • It helped.thanks .@wire(getSingleSitevisit,{recrdId: '$recordId'}) WireContactRecords({error, data}){ if(data){ console.log('DataTestingSiteVisit@@==>'+JSON.stringify(data)); this.wiredRecord=JSON.stringify(data); Here I am getting all thr required fields in the data but I cant get update them in the grant object on giving this condition.... this.Grant.Date_of_Visit__c=this.wiredRecord.Date_of_Visit__c; this.Grant.Details__c=data.Details__c; this.Grant.Flags__c=this.wiredRecord.Flags__c;
    – user110290
    Feb 24, 2022 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .