Accessing labels dynamically allows you to select different labels based on the data. Accessing labels dynamically also allows you to show the label in a different language than the user's default language.
Additionally, you can check if the translation for a particular language exists or not.
Static way of accessing Custom Label in Apex
String s2 = System.Label.MyLabelName; System.debug(s2);
A dynamic way of accessing Custom Label in Apex
Pass a blank string for the default namespace (c
) or when no
namespace is there.
System.debug(System.Label.get('', 'MyLabel'));
Pass the namespace if you want to access a label from a particular namespace. Please note that you can’t access labels that are protected in a different namespace.
System.debug(System.Label.get('my_package', 'MyLabel'));
Check if a translation exists for the label
Label.translationExists(namespace, label, language)
System.InvalidParameterValueException
Please note that both the methods System.Label.get()
and
Label.translationExists()
throw the below error if an invalid label
name is passed.
"System.InvalidParameterValueException: Invalid or non-existent label: MyLabel2"
cscsd
ReplyDeleteDear Anonymous, please help me understand what you are saying. Thanks
Deletesdgsg
ReplyDelete