2

How to read the Parent object field value in LWC, In the Below Soql fetching account.name, While reading in JS It's prints undefined

Apex:

    @AuraEnabled (cacheable = true)
  public static List<Contact> serviceCenterDetails(Id EnrollmentId) {          
    system.debug('ENROL ' + EnrollmentId);              
    return[SELECT AccountId, Account.name  FROM Contact WHERE User__c IN (SELECT  User__c  FROM 
   Enrollment__c WHERE Id = 'a082w000000ZR4x') limit 1];
}

JS:

@wire(serviceCenterDetails, { EnrollmentId: "$recordId" })
getservicecenter({ data, error }) {
if (data) {     
  let rtValues = Object.values(data); 
  for (let i = 0; i < rtValues.length; i++) {
    console.log("i -->" + i);
    console.log(
      "Id -->" + rtValues[i].AccountId
    );
    console.log(
      "Name -->" + rtValues[i].Account.name
    );

  }
}else {
  console.log("Svc Error  ===> " + JSON.stringify(error));
}
}

Console.Log Id --> 0012w000003cDFDAW Name -->undefined

Can you please help me on this

2

Apex is case-insensitive, but JS is not. JS is case-sensitive.

Instead of

console.log("Name -->" + rtValues[i].Account.name);

you should use Name upper-case first letter

console.log("Name -->" + rtValues[i].Account.Name);

| improve this answer | |
  • My bad:( Thanks @Oleksandr Berehovskyi – user3214361 Apr 2 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.