Saturday, December 20, 2014

Custom Labels as Global Variables

I haven't found the official Salesforce way for creating global variables or configuration properties yet, but this is what I found at one of my clients that I liked.

Say you want to create a global variable to store a public key with a value of "1234xyz".  Just create a new Custom Label with Name = "Global_PublicKey" and Value="1234xyz"



So in Apex you just reference the value as Label.Global_PublicKey:

  
private boolean isAuthorized(){
        String qPublickKey = ApexPages.currentPage().getParameters().get('publicKey');
        return qPublickKey.contains(Label.Global_PublicKey);
}


In a VisualForce page you refence it {!$Label.Global_PublicKey}:

  
<apex:page showheader="false" sidebar="false">
   <apex:pageblock title="Test">
   Global Variable PublicKey = {!$Label.Global_PublicKey}
   </apex:pageblock>
</apex:page>

No comments:

Post a Comment