0

I have hard-coded the fields in an LWC for my managed package queries:

   @wire(getRecord, { recordId: '$recordId', fields: ['Load__c.Id', 'Load__c.Distance_Miles__c', 'Load__c.Distance_Kilometers__c', 
                                                       'Load__c.Mileage_API_Borders__c',
                                                       'Load__c.Customer__r.Transportation_Profile__r.Mileage_API__c',
                                                       'Load__c.Customer__r.Transportation_Profile__r.Mileage_API_Version__c',
                                                       'Load__c.Customer__r.Transportation_Profile__r.Mileage_API_Routing__c'] })
    wiredRecord( result ) {

This blows up when I package the LWC in my managed package because of the namespace.

I believe the solution is to not hard-code the field names but to import them as field name imports are namespace aware.

OK but how do I import a namespace aware join field such as:

'Load__c.Customer__r.Transportation_Profile__r.Mileage_API_Routing__c'

Thanks!

2
  • Does it allow to import the reference fields like this? import ROUTING_FIELD from '@salesforce/schema/Load__c.Customer__r.Transportation_Profile__r.Mileage_API_Routing__c';, Shouldn't it start with Load__r?
    – Raul
    Jun 17, 2020 at 16:23
  • Thanks for helping, Raul!
    – Dave C
    Jun 17, 2020 at 19:52

1 Answer 1

4

Use an import statement, something like:

import MILEAGEAPIROUTINGFIELD from '@salesforce/schema/Load__c.Customer__r.Transportation_Profile__r.Mileage_API_Routing__c';

Then you can use that in a wired method:

@wire(getRecord, { recordId: '$recordId', fields: [MILEAGEAPIROUTINGFIELD])
mileageApiRouting;

The compiler will take care of your namespace discrepancies for you automatically.

Note: the "ns__name__c" model is only required for fields and objects outside of your own namespace.

3
  • Great! Thanks as always, sfdcfox!!
    – Dave C
    Jun 17, 2020 at 19:53
  • @sfdcfox Is there a best practice for referencing these fields when the wire function is not "robust" enough for the use case? An example is instead of simply fetching a record, I need to perform some logic in apex and return a wrapper class with some records (of a namespace object type) included. Is the best way to reference the fields on these returned records just to add the namespace to the reference in lwc?
    – Rory
    Jan 6, 2022 at 20:23
  • @Rory import statements can be used to get the correct value regardless of using wire or not. I'd have to set up an example to look at it under the hood, but I'd say an import statement should be preferred if you can. If it's outside your own namespace, definitely always use the namespace prefix.
    – sfdcfox
    Jan 6, 2022 at 23:41

You must log in to answer this question.

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