Use Schema Global Variables for Field Labels

VisualForce is on its way out. Lightning is just about mandatory, and JavaScript all the things! Except, there’s still about 9,000 trillion VisualForce pages out there to maintain. To help reduce the amount of time you spend going back and updating field labels, use this trick to dynamically get the correct label for the field in question (so if an admin changes the label, it automatically reflects the change on the VisualForce page). Hot dog! Now, you can spend more time writing those Lightning Components.

Here’s an example of a page where two developers talk about the same field: Account.Demo_Field__c, but one uses static text and one uses $ObjectType to create a schema reference to the label…

When rendered, this code looks like…

So far so good. But what if some ambitious admin changed the label for Account.Demo_Field__c so that it no longer matches the API name (kind of a no-no, but it happens)? If they change it to “The New Name for Demo Field” the page will now display:

Developer 1 has some work to do. They need to go back and update their page to refer to the new Account.Demo_Field__c label correctly. What about Developer 2? They’re done! The schema variable they used automatically reflected the label change, so they can go on their merry way and work on something else! So be like Developer 1 by learning more about using $ObjectType here.

Posted in Learning