To access labels in a Lightning web component, import them from the
@salesforce/label
scoped module.import greeting from '@salesforce/label/c.greeting';
import salesforceLogoDescription from '@salesforce/label/c.salesforceLogoDescription';
As far as the official documentation goes, it is clear that one needs to know the name of a custom label at design time; it is impossible to evaluate the value at runtime by knowing their name.
Arteezy and Sfdcfox commented in this StackExchange post that you can use Salesforce inspector and tooling API to retrieve the custom labels using this query:
SELECT Id, Name, Value FROM CustomLabel
Based on this, I have created an LWC service component
customLabelService
, which will retrieve all the custom labels and store it as an object on client-side. You can use this component as below:1. As per this StackExchange post, there is no way of getting a valid API-capable sessionId from a Lightning component because sessions obtained from Apex used by a Lightning component are not API enabled. That's why as a workaround, I have created a VF page, and
getSessionIdFromVFPage
method in CustomLabelController
class to retrieve the sessionId. You can also refer to this post to know more in details.2. If there is a requirement where you need to retrieve all the labels with a known prefix, then change the resourcePath (URI) to below:
/services/data/v46.0/tooling/query/?q=Select+id,Name,Value+from+CustomLabel+WHERE+Name+LIKE+'OAuthModal%25'
where the prefix is
OAuthModal
. I checked in Workbench, and this works fine, but you will need to tweak the code to pass the prefix.3. As per this StackExchange post, there are two objects that represent labels,
CustomLabel
and ExternalString
. So you can also use tooling API to fetch the labels from ExternalString
using the below query:SELECT Id, Name, Value, Language FROM ExternalString
UPDATE:
Please refer the code of this project from the link below:
https://github.com/iamsonal/Custom-Labels
0 Comments