0

There are several topics related to this issue:

I tried the different solutions suggested and all of them have worked correctly.

  1. Specify the sobjectype parameter as stated in this answer.
const record = {
  sobjectType: "Account",
  Name: "Demo",
  Phone: "11111111"
};
  1. Build an object where the name of their properties matches the API Name of the field being referenced.

    1. The property name is set as a string as stated in this Gist.
    2. Using @salesforce/schema as stated in this answer.
First example:
const record = {
  Name: 'Demo',
  Phone: '11111111'
};
Second example:
import ACCOUNT_NAME from "@salesforce/schema/Account.Name";
import ACCOUNT_PHONE from "@salesforce/schema/Account.Phone";

const record = {
  [ACCOUNT_NAME.fieldApiName]: 'Demo',
  [ACCOUNT_PHONE.fieldApiName]: '11111111',
}

However, if you try to access sObject fields through relationships it doesn't work. Accessing the sObject field passed to the Apex controller the value is null.

You can specify up to 5 levels of spanning fields. For example, Opportunity.Account.CreatedBy.LastModifiedById returns 4 levels of spanning fields.

import ACCOUNT_NAME from "@salesforce/schema/Account.Name";
import CONTACT_EMAIL from "@salesforce/schema/Account.Contact.Email";

const record = {
  [ACCOUNT_NAME.fieldApiName]: 'Demo',
  [CONTACT.EMAIL.fieldApiName]: '[email protected]',
}

Why are the related fields not received by the Apex controller?

Generally, an sObject is passed as a parameter to perform some DML action from the controller. In such cases the related fields don't make sense.

In some cases DML actions are not needed, but the sObject and its related fields are necessary, and try to avoid:

  1. Run a query in the controller since the record has already been retrieved in the LWC.
  2. Wrapper Class in the Apex controller.
  3. Method with too many parameters.
3
  • You don't need flat field paths for related objects, you actually need properties that are objects. E.g. { Name: "Demo", Contact: { FirstName: "Fred", Id: "003..." }}.
    – Phil W
    Commented Mar 25 at 23:17
  • The example works well. In order to clarify, Id field of the related object has to be part of object structure (as stated in your example). The following scheme would be incorrect; { Name: "Demo", Contact: { FirstName: "Fred" } } because the contact related object doesn't include the Id field. For further information on how to pass data to Apex along retrieved data from the wire adapter getRecord, read "Unable to read Sobject" when passing LWC getRecord data to Apex.
    – Tzinm
    Commented Mar 26 at 21:32
  • I'll leave you to add a self-answer :)
    – Phil W
    Commented Mar 26 at 22:28

1 Answer 1

0

@phil-w suggested solution in his first comment works well. Instead flatten field paths for realted objects, the key will be to build a new nested object of the related object. An example will help to illustrate this.

const record = {
  Name: 'Demo',
  Contact:
  {
    Id: '003...',
    Email: '[email protected]'
  }
}

It is worth mentioning regarding the previous example that the Id field has to be part of object structure. The following scheme would be incorrect because the contact related object doesn't include the Id field.

const record = {
  Name: 'Demo',
  Contact:
  {
    Email: '[email protected]'
  }
}

Perhaps a record that will be passed to the Apex controller will be one retrieved from the wire adapter getRecord. If so, consider reading the question "Unable to read Sobject" when passing LWC getRecord data to Apex.

You must log in to answer this question.

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