11 May 2011

Parameterized Custom Label

I still remember when custom labels where new and I have a requirement in which I wanted to create a VF email template in which non technical people wouldn't need to edit VF pages to update the email content.

Custom labels first came to mind. However, I thought that multiple custom labels will do the work in which I will concatenate each of them included values I wanted to insert in between words. Then I found out that this is not the best approach as SFDC impose limits to the number of custom labels per org.

Then I found what I'm looking for. It turns out that I can add parameter to custom labels and use the <apex:param> tag to update the value of the parameter in custom labels. To make the story short I created a sample for this entry.

First we create a custom label similar to this:


Take note of the values denoted by the curly braces and the number inside of it. This values empowers the custom labels to accept parameters. Also, you noticed that it start with the number 0. This is the default, as the first parameter you will add on your VF pages <apex:outputText> or any tags that will be displayed on that merge field (I'm calling it merge field).

Next we create a simple VF email template.


For the purpose of this example, I just created an email template that uses the recipientType and relatedToType to Contact. Basically I used the <apex:outputText> tag and add the custom label as its value. Then inside the <apex:outputText> tag, I added some <apex:param> tags with values. Note that the first param tag will be the value for the {0} merge field.

When you save the VF email template you will instantly see that the parameters was substituted by the values you added on the tags.

That's it. You have used parameters for your custom labels!