The Trim+Concat Hack
Why is this hack needed? Let's say you are trying to compile a list of names using records from the Contacts dataverse table. But maybe some of the contact have an academic title. And if they do, you'd like to prepend that title to their names.The straight-forward way would be to use an if() expression, similar to this:
if(
equals(
empty(
items('Apply_to_each')?['academic_title']
),
true
),
items('Apply_to_each')?['fullname'],
concat(
items('Apply_to_each')?['academic_title'],
' ',
items('Apply_to_each')?['fullname']
)
)
... a long and messy expression, especially due to the branching. This may also be hard to debug...
Instead use trim(). Wait... trim?? Yep, you heard right. The following simplified expression is only about 50% the size and much easier to read.
trim(
concat(
items('Apply_to_each')?['academic_title'],
' ',
items('Apply_to_each')?['fullname']
)
)
You probably figured it out. The trim()'s job is to eliminate the whitespace introduced by concat(), should the academic title be empty.
For longer concatenation chains, you may have to approach it in chunks of two, since trim can only eliminate a whitespace if it's at the beginning or end of a string.
Coalesce
If you haven't done so, also check out coalesce()! Like our hack above, coalesce() can make your expression easier to read and understand. You would use it, when your input string may be empty (again), but if it's empty, coalesce() let's you provide a replacement value.
coalesce(inputValue, replacement_value_if_inputArgument_is_empty)
You can even chain multiple arguments together and coalesce will pick the first argument that is not empty. E.g. with a hard-coded string at the end to signal that all of the arguments were empty.
coalesce(firstArgument, secondArgument, thirdArgument,'NO_VALUE')