OK, the main part of the answer turns out to be...
Put your LWC in an Aura component that implements lightning:isUrlAddressable
(see ref)
Here's an example with a Custom Metadata Type called Region__mdt
. You can access it via the relative URL /lightning/cmp/c__Region?c__apiName=YOUR_IDENTIFIER_HERE
:
Region.cmp (Aura)
<aura:component implements="lightning:isUrlAddressable" description="Region: {!v.apiName}" >
<aura:attribute name="apiName" type="String" />
<aura:handler name="init" value="{!this}" action="{!c.onPageReferenceChange}"/>
<aura:handler name="change" value="{!v.pageReference}" action="{!c.onPageReferenceChange}"/>
<!-- Embed the LWC like this -->
<c:regionDetail apiName="{!v.apiName}" />
</aura:component>
RegionController.js (Aura)
({
onPageReferenceChange: function(component) {
var myPageRef = component.get('v.pageReference');
component.set('v.apiName', myPageRef.state.c__apiName);
}
})
regionDetail.html (LWC)
<template>
<header class="slds-page-header slds-page-header__row slds-page-header__col-title slds-media slds-m-bottom_medium">
<lightning-icon class="slds-media__figure" icon-name="custom:custom78"></lightning-icon>
<h1 class="slds-media__body slds-page-header__name slds-page-header__name-title slds-page-header__title">
{region.Label}
</h1>
</header>
<section class="slds-box slds-theme_default">
<dl class="slds-list_stacked">
<dt class="slds-item_label slds-text-color_weak slds-truncate">Region ID</dt>
<dd class="slds-item_detail slds-truncate slds-m-bottom_small">{apiName}</dd>
<dt class="slds-item_label slds-text-color_weak slds-truncate">Area Factor</dt>
<dd class="slds-item_detail slds-truncate slds-m-bottom_small">{region.Area_Factor__c}</dd>
</dl>
</section>
</template>
regionDetail.js (LWC)
import { LightningElement, api, track, wire } from 'lwc';
import getRegion from '@salesforce/apex/RegionController.getRegion';
export default class RegionDetail extends LightningElement {
@api apiName;
@track region = {};
@wire(getRegion, { developerName: '$apiName' })
loadRegion(response) {
if (response && response.data) {
this.region = response.data;
}
}
}
RegionController.cls (Apex)
public with sharing class RegionController {
@AuraEnabled(cacheable=true)
public static Region__mdt getRegion(String developerName) {
return [SELECT DeveloperName, Label, Area_Factor__c
FROM Region__mdt WHERE DeveloperName = :developerName];
}
}
To get the Custom Metadata Type itself (including its fields), you use the CustomObject
metadata type. The API name just uses __mdt
instead of __c
.
sfdx force:source:retrieve -m CustomObject:My_Custom_Metadata_Type__mdt
+edit:
update for the sf
executable that Salesforce is pushing to replace sfdx
sf project retrieve start -m CustomObject:My_Custom_Metadata_Type__mdt
Best Answer
I would prefer Custom Setting for your requirement. As per Salesforce guidelines below are purpose of all three
Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user.All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, flows, Apex, and the SOAP API.
Custom metadata is customizable, deployable, packageable, and upgradeable application metadata. It mainly used to define for custom application development.
Custom labels enable developers to create multilingual applications by automatically presenting information in a user's native language.
So ideally Custom Setting is used for data setting.