0

I have error messages that are in the form System.Label.MyLabel1 + 'some other dynamic string' + System.Label.MyLabel2. Issue is I must have a space each between these 3 components. When I put a trailing space in System.Label.MyLabel1 value or a leading space in System.Label.MyLabel2 value, they are truncated by Salesforce when the custom label is saved.

I tried   and also read this post but it is not at all clear to me.

I can change the places (about hundreds of them) and put manually two spaces inside apex code itself. But before than just want to explore the options and if there is really no way out.

Note: I need the correct string in Apex, this is not a VF issue, so the posts stating escape=false for apex:outputText will not work for me.

1

Fields are naturally trimmed of white space when stored to the database. As such, you can't include a space at the beginning or end of a label. There's a few silly tricks, such as using the non-printable-character "" (character code 127), but as you can see, it appears as a double-width space, so it's not always practical. It may also show up as something else other than white space on some fonts (e.g. it might look like "□" or "�" on some systems). It's much better to use white space later. One trick you can use to help you out here is to use String.join instead:

String output = String.join(
    new String[] { Label.MyLabel1, 'some other dynamic string', Label.MyLabel2 }, ' ');

This may be particularly useful when you don't know how many strings you'll be joining together. You could also use String.format:

String output = String.format(
  '{0} some other dynamic string {1}',
  new String[] { Label.MyLabel1, Label.MyLabel2 });

This lets you format the string precisely with positional placeholders, and is useful when you do know how many parameters you have. It is a little more verbose, but you get extra flexibility. You could even use the format string as another label:

String output = String.format(
  Label.FormattedText,
  new String[] { Label.MyLabel1, Label.MyLabel2 });

Note that this last example is particularly useful, because this lets you translate the formatting in to different languages. For example, some languages you might end up writing "{1} ... {0}", effectively swapping the two labels around, because of the difference in sentence structure. Some languages, like German, have different rules for spacing, too, so you might not want the extra spaces to be hard-coded in a string.

  • Thanks a lot for all the options. I went ahead with join as it is so simple and so very effective!:) – SFDC Dev 27 Feb 28 '18 at 11:51
1

You can also do this using this way

System.Label.MyLabel1 + ' some other dynamic string ' + System.Label.MyLabel2 //Add the space in string instead of label.

using this you don't need to worry about any space and if you change the label position this will still work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.