2

I don't know how to ask this question properly - so please bear with me.

Let's say I query an account via getRecord:

@wire(getRecord, { recordId: '$recordId', fields: ['Account.Id', 'Account.Name'] })
wiredRecord({ error, data }) {
}

And one (or more) via a custom Apex method getAccounts:

@wire(getAccounts, { accountId: '$recordId' })
wiredAccounts({ error, data }) {
}

With getRecord I access the Name of the account like this:

data.fields.Name.value

Via the Apex query, I access the Name of the first account like this:

data.[0].Name

Two major differences here - first of all, I don't have to access the fields of an object via .fields, and second of all - I don't have to use .value to get the field's value. Why is that? Why will getRecord return an object in this special wrapper? And why doesn't my Apex query?

The follow-up code doesn't care where the account is coming from but I have to handle it differently because it's returned differently. Surely there's a reason for that and an explanation in the docs somewhere? Can anybody shed some light on this? If so, and you have this "issue" as well - what's your solution?

If this question is not well-written - please help me to improve it and not just downvote it. Thanks!

1 Answer 1

7

The difference in structure is simply that the data is returned differently by these two services (one standard and one custom).

Your custom service is probably returning simple SObject data, which is essentially a JSON representation of records, related record lists and fields with values.

On the other hand, the Get Record wire returns a whole bunch of additional information. You can read about the structure in the documentation.

For field.value have a look here where you'll see that a field provides both the "raw" value and the display value:

Property Type Description
displayValue String The displayable value for a field.
value Object The value of a field in its raw data form.

The returned data from both is accessed as JavaScript objects. You could write some JS code to traverse the Get Record response and turn it into the same structure as a simple SObject. Just make sure to create a new JavaScript object from it - both return "immutable" (Read only) values.

3
  • Is it possible to convert a User Interface API Response Body into a simple object? Does such a feature exist? Or can I annotate my query so that it returns such an object?
    – Semmel
    Commented Mar 18 at 18:18
  • 1
    @Semmel you would need to build a custom wrapper in your apex class that mirrors (some or all) of what GetRecord does, and then return that to the LWC instead of just returning the List<SObject>. Or if you are building a reusable component and you can't anticipate if the data will come from GetRecords or Apex, you can build a JS function to convert the generic Apex return into a GetRecords style wrapper. Commented Mar 18 at 18:53
  • This is precisely what I'm doing at the moment - I just wondered if this is a problem that also has a "standard" solution. But a custom wrapper or conversion is good enough. :)
    – Semmel
    Commented Mar 19 at 12:22

You must log in to answer this question.

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