3

I am building LWC component to include it in Case Record page. My lwc component should display current Case Owner Name. As per Salesforce LWC wire Service Ref, here is the way to fetch reference/lookup records:-

import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';

This syntax works well in VS code for Account, Opportunity, Contact etc.

But following doesn't work:-

import OWNER_NAME_FIELD from '@salesforce/schema/Case.Owner.Name';

It also doesn't work with Lead. So assumption is, may be both record can be owned by Queue also so it throws error while saving in VS Code.

Although I can use Lightning Base component (Lightning-record-form) OR using apex callout to get owner name for record but since SF already gives option to use wire service to fetch lookup record, I would like to use same for Case object. Does anyone has clue why Case and Lead aren't allowed to fetch owner name.

3

Correction in your question:

here is the way to fetch reference/lookup records

You are not really fetching records. You are getting the definitions of fields through imports.

Problem:

It seems there are bugs for importing fields definitions for some fields/objects like in your case Case/Lead Owner Name. Also you cannot import the field definition for Geolocation fields - for example aLocation__longitude__s and aLocation__latitude__s

Solution:

Until we get a fix from salesforce, You can directly use these fields like 'Case.Owner.Name' in wire service as shown in below sample:

import CASE_SUBJECT_FIELD from '@salesforce/schema/Case.Subject';
import CASE_ACCOUNT_FIELD from '@salesforce/schema/Case.Account.Name';
import CASE_CONTACT_FIELD from '@salesforce/schema/Case.Contact.Name';

export default class Poc extends NavigationMixin(LightningElement) {
    @api recordId;

    @wire(getRecord, { recordId: '$recordId', fields: ['Case.Owner.Name', CASE_SUBJECT_FIELD, CASE_ACCOUNT_FIELD, CASE_CONTACT_FIELD] })
    gAcc({ data, error }) {
        console.log('Acc data => ', JSON.stringify(data), JSON.stringify(error), this.recordId);
    }
}

Output:

{
  "apiName": "Case",
  "childRelationships": {

  },
  "fields": {
    "Account": {
      "displayValue": "Edge Communications",
      "value": {
        "apiName": "Account",
        "childRelationships": {

        },
        "fields": {
          "Name": {
            "displayValue": null,
            "value": "Edge Communications"
          }
        },
        "id": "00128000009j45lAAA",
        "lastModifiedById": "00528000001IIBvAAO",
        "lastModifiedDate": "2015-11-16T09:57:33.000Z",
        "recordTypeInfo": null,
        "systemModstamp": "2017-12-11T18:45:20.000Z"
      }
    },
    "Contact": {
      "displayValue": "Rose Gonzalez",
      "value": {
        "apiName": "Contact",
        "childRelationships": {

        },
        "fields": {
          "Name": {
            "displayValue": null,
            "value": "Rose Gonzalez"
          }
        },
        "id": "00328000008ZUISAA4",
        "lastModifiedById": "00528000001IIBvAAO",
        "lastModifiedDate": "2015-11-16T09:57:33.000Z",
        "recordTypeInfo": null,
        "systemModstamp": "2015-11-16T09:57:33.000Z"
      }
    },
    "Owner": {
      "displayValue": "Sasank VS",
      "value": {
        "apiName": "Name",
        "childRelationships": {

        },
        "fields": {
          "Name": {
            "displayValue": null,
            "value": "Sasank VS"
          }
        },
        "id": "00528000001IIBvAAO",
        "lastModifiedById": null,
        "lastModifiedDate": null,
        "recordTypeInfo": null,
        "systemModstamp": null
      }
    },
    "Subject": {
      "displayValue": null,
      "value": "Starting generator after electrical failure"
    }
  },
  "id": "500280000068Gv5AAE",
  "lastModifiedById": "00528000001IIBvAAO",
  "lastModifiedDate": "2015-11-16T09:57:33.000Z",
  "recordTypeInfo": null,
  "systemModstamp": "2015-11-16T09:57:33.000Z"
}
  • Thank you @salesforce-sas.It works. Yes, there are some open issues with accessing related fields of few object. I used the response JSON to set the owner value in my html:- get ownerName() { return this.Case.data.fields.Owner.value.fields.Name.value;} – Aaditya Sep 12 at 13:27
  • If the issue is solved, pls mark the answer solved. Please check meta.stackexchange.com/questions/5234/… – salesforce-sas Sep 13 at 7:33

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.