SBX - Search With Button

SBX - Forum Post Title

Liquid: How to handle null in result of fetchXML

Microsoft Dynamics CRM Forum

Usmani asked a question on 9 Jan 2019 12:04 AM

Question Status

Verified
{% fetchxml my_query %}
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" returntotalrecordcount="true">
.....
.....
<filter type="and">
        <condition attribute="asc_id" operator="eq" uitype="new_uitype" value="{{request.params.id}}" />
      </filter>
    </link-entity>
  </entity>
</fetch>

I am using fetchXML with liquid in web template for ADX portal. The code below works fine when I provide valid ID

{% if my_query.results.entities %}
  {{ my_query.results.entities.size }}
{% else %}
  <span class="alert">No session found for this booking</span>
{% endif %}

However, when ID is not in CRM i.e. null result I am getting this error 

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


Could you please tell me how to handle null result in Liquid with fetchXML. I have tried following options

{% if my_query.results.entities %}
{% if my_query.results.total_record_count %}
{% if my_query.results %}

all return same error
Reply
Ravi Kashyap responded on 9 Jan 2019 12:21 AM
Suggested Answer

Hi,

I don't think you are getting this error because of the fetchxml is null, I think it is happening because of the error in the query itself due to the null value="{{request.params.id}}" ..

I would suggest to put a check condition for this and only execute fetch if there is a valid id.

Hope this helps.

Reply
Mahender responded on 9 Jan 2019 12:28 AM
My Badges

Agree with Ravi, my_query.results.entities should handle results for null as well

Reply
Usmani responded on 9 Jan 2019 12:51 AM

Request.params.id is not null. For testing I am just changing value. For instance, if right value is id=1, I have tried id=12 for testing and I am getting that error. Not sure if liquid can handle null.

Reply
Ravi Kashyap responded on 9 Jan 2019 1:03 AM
Verified Answer

Hi,

As mentioned above I don't think it is not about liquid not handling null. Its is about the value in the id …So for example, if you are using the request.params.id in the fetch xml filter criteria which will accept guid. Now if you pass non guid then the fetch xml itself will throw an error. This will happens outside of liquid as well if you try it in C# code.

Hope this helps.

Reply
Ravi Kashyap responded on 9 Jan 2019 1:03 AM
Verified Answer

Hi,

As mentioned above I don't think it is not about liquid not handling null. Its is about the value in the id …So for example, if you are using the request.params.id in the fetch xml filter criteria which will accept guid. Now if you pass non guid then the fetch xml itself will throw an error. This will happens outside of liquid as well if you try it in C# code.

Hope this helps.

Reply
Ravi Kashyap responded on 9 Jan 2019 12:21 AM
Suggested Answer

Hi,

I don't think you are getting this error because of the fetchxml is null, I think it is happening because of the error in the query itself due to the null value="{{request.params.id}}" ..

I would suggest to put a check condition for this and only execute fetch if there is a valid id.

Hope this helps.

Reply

SBX - Two Col Forum

SBX - Migrated JS