FetchXML Tutotorial

Order By in Fetch XML Dynamic CRM

In this article, we will learn Order By in Fetch XML Dynamic CRM.

In this blog, we will learn Order By in Fetch XML Dynamic CRM.
Order By in Fetch XML Dynamic CRM
The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the descending=”true”.

Syntax :

<fetch mapping="logical" version="1.0">
  <entity name="customers">
    <all-attributes />
    <order attribute="attribute_name" />
  </entity>
</fetch>

Customer Entity Database Record:
order-by-fetch-xml-dynamic-crm

The following fetchxml statement selects all customers from the “Customers” entity, sorted by the “Country” column:
Please Open fetchxml tester and execute this query.
OR Example :
Key Point:
vnks_customer:It is a logical name of entity.
vnks_country,vnks_city:It is a logical name of attribute/field.
vnks_address:It is a logical name of attribute/field.
vnks_postalcode: It is a logical name of attribute/field.

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
    <entity name="vnks_customer" >
        <attribute name="vnks_name" />
        <attribute name="vnks_address" />
        <attribute name="vnks_city" />
        <attribute name="vnks_postalcode" />
        <attribute name="vnks_country" />
        <order attribute="vnks_country" />
    </entity>
</fetch>

Result:

<result>
        <vnks_name>
            Blauer See Delikatessen
        </vnks_name>
        <vnks_address>
            Forsterstr. 57
        </vnks_address>
        <vnks_city>
            Mannheim
        </vnks_city>
        <vnks_postalcode>
            68306
        </vnks_postalcode>
        <vnks_country>
            Germany
        </vnks_country>
        <vnks_customerid>
            {EBE9D28C-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Rajesh Singh
        </vnks_name>
        <vnks_address>
            Lucknow
        </vnks_address>
        <vnks_city>
            Lucnow
        </vnks_city>
        <vnks_postalcode>
            876567
        </vnks_postalcode>
        <vnks_country>
            Germany
        </vnks_country>
        <vnks_customerid>
            {A947CC22-2740-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Kishan Yadav
        </vnks_name>
        <vnks_address>
            Varansai
        </vnks_address>
        <vnks_city>
            Varanasi
        </vnks_city>
        <vnks_postalcode>
            23457
        </vnks_postalcode>
        <vnks_country>
            India
        </vnks_country>
        <vnks_customerid>
            {DEBBC9B8-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Karuna
        </vnks_name>
        <vnks_address>
            Buxar
        </vnks_address>
        <vnks_city>
            Buxar
        </vnks_city>
        <vnks_postalcode>
            897645
        </vnks_postalcode>
        <vnks_country>
            India
        </vnks_country>
        <vnks_customerid>
            {0AF7F5CE-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Karuna
        </vnks_name>
        <vnks_address>
            Buxar
        </vnks_address>
        <vnks_city>
            Buxar
        </vnks_city>
        <vnks_postalcode>
            908767
        </vnks_postalcode>
        <vnks_country>
            India
        </vnks_country>
        <vnks_customerid>
            {D1EFDAE7-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Ana Trujillo Emparedados y helados
        </vnks_name>
        <vnks_address>
            Avda. de la Constitución 2222
        </vnks_address>
        <vnks_city>
            México D.F.
        </vnks_city>
        <vnks_postalcode>
            05021
        </vnks_postalcode>
        <vnks_country>
            Mexico
        </vnks_country>
        <vnks_customerid>
            {B3A6F1A9-2540-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
   
 

ORDER BY DESC Example
The following fetchxml statement selects all customers from the “Customers” entity, sorted DESCENDING by the “Country” attribute:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
    <entity name="vnks_customer" >
        <attribute name="vnks_name" />
        <attribute name="vnks_address" />
        <attribute name="vnks_city" />
        <attribute name="vnks_postalcode" />
        <attribute name="vnks_country" />
        <order attribute="vnks_country" descending="true" />
    </entity>
</fetch>

Result:

<result>
        <vnks_name>
            Maria Anders
        </vnks_name>
        <vnks_address>
            Obere Str. 57
        </vnks_address>
        <vnks_city>
            Berlin
        </vnks_city>
        <vnks_postalcode>
            235767
        </vnks_postalcode>
        <vnks_country>
            US
        </vnks_country>
        <vnks_customerid>
            {EA128477-2540-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Ravi
        </vnks_name>
        <vnks_address>
            Varanasi
        </vnks_address>
        <vnks_city>
            Varanasi
        </vnks_city>
        <vnks_postalcode>
            987689
        </vnks_postalcode>
        <vnks_country>
            US
        </vnks_country>
        <vnks_customerid>
            {CB0C7FFC-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Around the Horn
        </vnks_name>
        <vnks_address>
            120 Hanover Sq.
        </vnks_address>
        <vnks_city>
            London
        </vnks_city>
        <vnks_postalcode>
            WA1 1DP
        </vnks_postalcode>
        <vnks_country>
            UK
        </vnks_country>
        <vnks_customerid>
            {5406F211-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Berglunds snabbköp
        </vnks_name>
        <vnks_address>
            Berguvsvägen 8
        </vnks_address>
        <vnks_city>
            Luleå
        </vnks_city>
        <vnks_postalcode>
            S-958 22
        </vnks_postalcode>
        <vnks_country>
            Sweden
        </vnks_country>
        <vnks_customerid>
            {E881A74B-2640-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>
    <result>
        <vnks_name>
            Ana Trujillo Emparedados y helados
        </vnks_name>
        <vnks_address>
            Avda. de la Constitución 2222
        </vnks_address>
        <vnks_city>
            México D.F.
        </vnks_city>
        <vnks_postalcode>
            05021
        </vnks_postalcode>
        <vnks_country>
            Mexico
        </vnks_country>
        <vnks_customerid>
            {B3A6F1A9-2540-EA11-A812-000D3AF02D13}
        </vnks_customerid>
    </result>

Any question and suggestion related to this topic Order By in Fetch XML Dynamic CRM please comment. Thank you

Leave a Reply

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