Power Pages

Use Liquid and Fetch XML to retrieve a single record without needing a GUID

So you might be wondering why would we use fetch XML to query a single record when we’ve got the super easy to use entities object? Well, the difference is that with the entities object, we have to know the GUID, we either have to have queried for it first or we need to have it provided in the URL so that we can use that for the query.

Whereas we’ve using Fetch XML to retrieve a single record. We’ve got much more flexibility. So essentially anything that we do in advanced find we’ll just take it one step further and we’ll apply a particular sort order. So for example, of all the tracks, I want to see them in descending order, by when they were created telling me which is the most recent track.

So sort order is one very important aspect and the other, as you can see highlighted there is I’ve had to specify in the first line of that fetch XML, how many records I want returned. So I want the top one. Okay, but just remember top doesn’t particularly mean anything. It just means that the first result.

So you have to specify with the sort order, what do you mean by top? Do you mean the most recent, the most popular, the highest value, the lowest cost. So remember the sort order is vital.

{% fetchxml most_recent_track_query %}
    <fetch version="1.0" output-format="xml-platform" mapping="logical" top="1">
        <entity name="musdyn_track">
            <attribute name="musdyn_name" />
            <attribute name="createdon" />
            <order attribute="createdon" descending="true" />
            <filter type="and">
            <condition attribute="statuscode" operator="eq" value="1" />
            </filter>
        </entity>
    </fetch>
{% endfetchxml %}
Franco Musso

You may also like

Leave a reply

Your email address will not be published.

More in Power Pages