function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prakashedlprakashedl 

Unable to access labels within managed package

I create a managed package myself in one org and installed it in another org. The managed package is in beta status. The managed package has a few custom labels defined that I would like to use it in my apex code in the subscriber's organization. The labels are NOT protected. They have the protected checkbox unchecked. But still I am unable to access them from apex using Label.namespace__labelname. I always get a compilation error "invalid external string name". Am I missing something?

rajesh yrajesh y

Can you share your code where you are facing the issue

Navatar_DbSupNavatar_DbSup

Hi,

 

You can’t do that in apex, however it can be done in visualforce page using global variable that you can provide the custom label name at runtime.

 

The following example illustrate this :

 

//-------------------- Controller-----------------------

public class MyController

{

public String mylabel{get; set;}

 

public MyController()

{

 mylabel='Test_label';   // provide custom label name as you have, in my case it is Test_Label

}

}

 

//----------------VF Page ------------------------

 

<apex:page Controller="MyController">

<apex:outputText> {!$Label[mylabel]} </apex:outputText>

</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

JP12345JP12345
Plz try this syntax:-
Label.namespace.labelname
Lindsay Recknell 7Lindsay Recknell 7


JP12345 was correct! I had the same problem and using a dot instead of a doubble underscore fixed the issue.

Label.namespace__labelname --> change to --> Label.namespace.labelname

Chris KettenbachChris Kettenbach
This was awesome. I needed to use a label in a different namespace in Apex at runtime. This tip was the key for my use case. Thanks!