+ Start a Discussion

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




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>



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

Plz try this syntax:-
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!