8

Is there a way to parameterize a custom label? Example:

Label name: MyTestLabel

Label value: Thank you, {0}, for your feedback.

Then in Apex, do something like this:

String name = 'John';
String label = Label.MyTestLabel;
String value = String.valueOf(label, name);

The idea being that the value variable would now hold "Thank you, John, for your feedback.

1 Answer 1

21

You can do this using the String.format() method which can provide parameter substitution on your label. The second parameter to this method is a List type and contains your parameters to be substituted.

Label name: MyTestLabel
Label value: Thank you, {0}, for your feedback.


List<String> parameters = new List<String>();
parameters.add('John');

String label = Label.MyTestLabel;
String value = String.format(label, parameters);

You can do it in one line like this as well:

string.format(Label.MyTestLabel, new String[]{'John'});
string.format(Label.MyTestLabel, new List<String>{'John'});
2
  • Thanks, I am not sure why I did not put two and two together on this. It worked perfectly.
    – user6861
    Jun 11, 2014 at 16:27
  • @JohnGaughan glad you got it worked out!
    – Mark Pond
    Jun 11, 2014 at 17:57

You must log in to answer this question.