Power pages adventures – Liquid error: Exception has been thrown by the target of an invocation

By | August 23, 2024

I’d say there is a disclaimer for this post – there might be a better way to do what I’m about to explain, but, well, maybe it’s the only way.

So, every now and then, you’d be using FetchXml with Liquid, and you’d be getting an error:

Liquid error: Exception has been thrown by the target of an invocation

This often just means that your FetchXml is broken, and it might be expected or not. If it is somewhat expected (possibly there is a dynamic parameter, and it can be empty), you may want to handle such errors gracefully instead of just dumping that error message to the page for your users to see.

Your liquid code would normally look like this:

{% fetchxml queryResult %}
...
{% endfetchxml %}

Problem is, if there is an error in the fetch, you can’t do anything with that queryResult variable. As soon as you try accessing the results, you’ll get an error mentioned above.

Turns out we can still use “capture” Liquid tag to capture the error, and, then, use capture variable in the condition to see if there was an error:

That does the trick, see below:

Leave a Reply

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