This post is inspired by Jessica Pasley who asked on Twitter how to take multiple fields on a record and combine them such that they displayed a grammatical sentence so that the resulting text can be used in merge templates.
And there were 15 fields.
And fields should be excluded if had certain values.
And different delimiters were needed.
It’s relatively simple to combine field values together with a common delimiter, but the challenge gets much harder when needing to use multiple delimiters and it’s variable when/where those delimiters will need to be used.
I had originally thought I could do this with a bunch of Decision elements in a Flow but that quickly grew out of hand, particularly when trying to build out Jessica’s requirements to support 15+ fields.
My approach in the end was to develop an Invocable Apex class that could be called from Flow to obtain the concatenated text. From there Flow could do what it wanted with the text. Most common use will be to update a record or could use it in an email template.
By implementing the solution as Invocable Apex this logic can be reused in Process Builder, Flow, REST API, and Apex triggers. Nice!
Check out the project on GitHub to deploy the sample app and code. The project comes with a sample Process Builder and Flow that show how to use the Invocable Apex class.