1

I've searched for an answer but found none. The answer is probably simple, but I'm tired of trying and not finding a solution! I need to access the fields returned from the query. It does not return an array, but a single record only. I cannot use getRecord as I don't know the object of the recordId - the component will sit on different flexipages so the recordId could be one of 3 different objects, which is why I run the query (the code example is very simplified, so don't look at the query etc - it returns a record so I know that all works as I need it to). My problem is getting hold of the field content returned.

Simplified code below: Apex controller:

@AuraEnabled(cacheable=true)
public static MyObject__c getMyRecord(
String objId
) {
  return [
    SELECT Id, Name
    FROM MyObject__c
    WHERE otherId = :objId
    LIMIT 1
  ];
}

LWC js:

import { LightningElement, api, wire, track } from "lwc";
import getMyRecord from "@salesforce/apex/MyController.getMyRecord";

export default class MyLWC extends LightningElement {
  @api recordId;
  @track record;
  @track recId;

  @wire(getMyRecord, {
    objId: "$recordId"
  })
  results({ error, data }) {
    if (data) {
      console.log("in @wire " + JSON.stringify(data));
      this.record = data.MyObject__c;
      this.error = undefined;
      console.log("record: ", this.record);
    } else if (error) {
      this.error = error;
      this.record = undefined;
    }
  }

  get myRecordId() {
    return this.record[Id];
  }
}

HTML:

<lightning-card title="MyLWC">
    <p>This is MyRecord : {record.Id} {record.Name}</p>
    <p>This is MyRecord : {myRecordId} </p>
    <p>This is MyRecord : {record[Id]} </p>
    <p>This is MyRecord : {record.data.Id} </p>
</lightning-card>

How do I get hold of the Id and the Name fields??

I've tried as the example above, which all throw errors. How do I write the HTML or the getter for Id and Name?

enter image description here

enter image description here

The json is correct:

enter image description here

UPDATE

Based on the answers, I changed the js to:

  this.record = data;
  this.error = undefined;
  this.recId = this.record.Id;
  console.log("record: ", this.recId);

And my HTML to:

This is MyRecord : {recId}

1
  • 2
    The data is the record, so this.record = data is what you need. BTW, why is the Apex returning type MyObject__c not SObject?
    – Phil W
    Mar 29, 2023 at 6:41

1 Answer 1

2

As you can see from the console log, data holds both Id and Name, it doesn't hold a MyObject__c object, so you should just assign data to this.record:

if (data) {
    console.log("in @wire " + JSON.stringify(data));
    this.record = data;
    this.error = undefined;
    console.log("record: ", this.record);
}

data will always hold whatever object returned by the apex method, no matter if it's a single record, a list or a primitive.

1
  • Thanks, I updated my post with what finally worked :)
    – Irene
    Mar 29, 2023 at 19:51

You must log in to answer this question.

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