Formatted Strings Enclosed within Single Quotes Unsupported

The other day I was receiving an error while running some dynamic SOQL code. The error was unexpected “{” in SOQL query. That was a new one for me. After troubleshooting for a while, I noticed that my formatting token wasn’t being replaced in String.format. Executing the following snippet in execute anonymous confirmed my suspicion: Formatting tokens enclosed within single quotes aren’t supported.

The following snippet shows that formatting works without single quotes.

To work around this, build the string by concatenating the pieces together yourself or useĀ Variable Binding in Dynamic SOQL.

EDIT: Jeff Hube has another solution: Escape the single quotes with another set of single quotes.

Jeff is a friend and co-worker and a very talented developer. I look forward to seeing his updated site, jeffhube.com, when it’s available.

Have you encountered other unexpected behavior with String.format? If so, let me know in the comments.

Happy Coding,

Luke

2 thoughts on “Formatted Strings Enclosed within Single Quotes Unsupported”

  1. Hey, Luke. I’ve run into this one as well. Another solution is to use two single quotes around the formatting token, like so: ‘… \’\'{0}\’\’ …’

    1. Hey Jeff.

      Thanks for the feedback. The post has been updated to reflect this additional solution.

Leave a Reply

Your email address will not be published. Required fields are marked *