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"
No comments :
Post a Comment
Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com