3

I have Custom Metadata Type values that I want to display in a Lighning Web Component. I've read a few different posts (post 1, post 2) about this but neither seems to work for my scenario.

My Custom Metadata Type (API name: Billing_Custom_MetaData__mdt) has a field (customerUrl) that I need to display in my component:

myLwcComponent.html

<template>
  <div class="slds-m-around_medium>
    <p>Web address: {customerUrlValue}</p>
  </div>
</template>

myLwcComponent.js

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
const FIELDS = [
  'Billing_Custom_MetaData__mdt.customerUrl__c',
];

export default class LifeCycle extends LightningElement {
  @api recordId;
  @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
  metaDataRecord;

  get customerUrlValue() {
    return this.metaDataRecord.data.fields.customerUrl.value;
  }
}

I'm new to LWC so I'm trying to follow those posts and Salesforce documentation (Get Record Data) for correct information. However, the customerUrl value from Billing_Custom_MetaData__mdt isn't rendering in my component.

3
  • Should the field be customerUrl__c or is that just a copy/paste typo? Nov 30, 2021 at 16:15
  • @KrisGoncalves good question. I can try that, past reading didn't indicate to include __c are part of the name. Looks like that didn't work either.
    – Matt Smith
    Nov 30, 2021 at 16:26
  • I would update your code in the post to customerUrl__c so it's clear for the next person that reads this. Something that would be helpful for you is to include a debugger statement inside of customerUrlValue() to see what metaDataRecord.data is when the method is called to verify that the record is being loaded correctly and then narrow down that the field value is being retrieved. Likely permissions will be a factor.
    – nbrown
    Nov 30, 2021 at 16:39

1 Answer 1

3

in wire method metaDataRecord you are passing recordid. please note that recordid you defined is calculated based on scope of page and it is not metadatatype record id. if it is a record detail page,it provides you recordid of that record.

you cannot use this recordid to fetch metadatatype records.

best option is querying metadata records using apex and call apex in lwc. (as per my limited knowledge of salesforce,this is best option)

if you want to avoid apex, you have to hardcode your metadatatype id in wire call if it works for you but definitely not recommended.

@wire(getRecord, { recordId: 'hardcodedi id of metadata record', fields: FIELDS })
  metaDataRecord;
1
  • and customerUrl should be replaced with api name of field in import and getter method as you discussed in comments Nov 30, 2021 at 16:58

You must log in to answer this question.

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