0
public with sharing class retriveaccndcon {
    @AuraEnabled
    
    public static list<account> getrecords(string parentId)
    {
        return [select id,name,(select Id,FirstName,LastName,Email from contacts) from account where Id=:parentId ];
    }
}

import { LightningElement,wire} from 'lwc';
import getrecords from '@salesforce/apex/retriveaccndcon.getrecords';
export default class Getrelatedrecordswithapex extends LightningElement {
    accountwithcontacts;
    @wire(getrecords,{parentId:'0015i00000utZOZAA2'})
    wirehandler({data,error})
    {
        if(data)
        {
            this. accountwithcontacts=data;
            console.log(data);
        }
        else if(error)
        {
            console.log('error..........',error);
        }
    }
}

<template>
    <lightning-card title="RETRIVE  ACCOUNT & CONTACT RECORDS WITH RECORDID">
        <temmplate if:true={accountwithcontacts}>
            <template for:each={accountwithcontacts} for:item="act">
                <li key={act.Id}>
                    {act.Id}
                    {act.Name}
                </li>
                <template for:each={act.contacts} for:item="con">
                    <li key={con.Id}> {con.Id}
                        {con.FirstName}
                        {con.lastName}
                    </li>
                </template>
            </template>
        </temmplate>
    </lightning-card>
</template>

this code is printing only title of the lightning card may I know what is the error in this code and how can I print the values of the related object fields with the recordId.enter image description here

1 Answer 1

0

The basic issue with your code is you are not annonating @AuraEnabled(cacheable=true) to your apex method. The wire function expect your apex method to be annonated with it.

this will fix your issue:-

public with sharing class retriveaccndcon {
     @AuraEnabled(cacheable=true)
    public static list<account> getrecords(string parentId)
    {
        return [select id,name,(select Id,FirstName,LastName,Email from contacts) from account where Id=:parentId ];
    }
}

Read more here:- https://developer.salesforce.com/docs/platform/lwc/guide/apex-wire-method.html

2
  • Thank you so much now its working Commented Oct 29, 2023 at 9:34
  • You can accept it as an answer. Commented Oct 30, 2023 at 1:20

You must log in to answer this question.

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