web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Order Type in Fetch XML

(0) ShareShare
ReportReport
Posted on by 105

Hi, 

Can any 1 help on how to include the order attribute based on conditions. Because I am reusing the fetchXML for multiple methods. so for each method I have to sort by specific attributes.

@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='asisanyuserassociation'>
<attribute name='asisanyid' />
<attribute name='amyorganization' />
<attribute name='aorgid' />
<attribute name='auserid' />
<attribute name='aid' />
<attribute name='aispreferred'/>
<attribute name='amysysid'/>
<attribute name='amydid'/>
<attribute name='ausercode'/>
<attribute name='asisanyuserassociationtype'/>
<link-entity name='auser' from='auserid' to='auserid' visible='false' link-type='outer' alias='CDAuserLnk'>
<attribute name='anamelocation' />
<attribute name='aid' />
<attribute name='asapcode'/>
<attribute name='amyorganization'/>
<attribute name='aorgid'/>
<link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAuserorgLnk'>
<attribute name='aname' />
<attribute name='aenum' />
<attribute name='aalias1' />
</link-entity>
</ link-entity>

<link-entity name='amysys' from='amysysid' to='amysysid' visible='false' link-type='outer' alias='CDAmysysLnk'>
<attribute name='aid' />
<attribute name='aserialnumber' />
<attribute name='aeqmodelid' />
<attribute name='aeqproductlineid' />
<attribute name='aorgid' />
<link-entity name='amysysmodel' from='amysysmodelid' to='aeqmodelid' visible='false' link-type='outer' alias='CDAmysysModelLnkEntity'>
<attribute name='aid' />
<attribute name='aname' />
</link-entity>
<link-entity name='amysysproductline' from='amysysproductlineid' to='aeqproductlineid' visible='false' link-type='outer' alias='CDAmysysProductLineLnkEntity'>
<attribute name='aid' />
<attribute name='aname' />
</link-entity>
<link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAmysysorgLnkEntity'>
<attribute name='aname' />
<attribute name='aenum' />
<attribute name='aalias1' />
</link-entity>
</link-entity>
<link-entity name='amyd' from='amydid' to='amydid' visible='false' link-type='outer' alias='CDAmydLnk'>
<attribute name='aid' />
<attribute name='asn' />
<attribute name='aname' />
<attribute name='amysysid' />
<attribute name='amydmodelid' />
<link-entity name='amydmodel' from='amydmodelid' to='amydmodelid' visible='false' link-type='outer' alias='CDAmydModelLnkEntity'>
<attribute name='apartnumber' />
<attribute name='aisconnected' />
<attribute name='aname' />
<attribute name='aorgid'/>
<link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAmydModelorgLnkEntity'>
<attribute name='aname' />
<attribute name='aenum' />
<attribute name='aalias1' />
</link-entity>
</link-entity>
</link-entity>

<link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAorgLnk'>
<attribute name='aname' />
<attribute name='aenum' />
<attribute name='aalias1' />
</link-entity>
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0'/>
</filter>
<filter type='and'>" + criteriaCheck+"</filter></entity></fetch>";

Please help...!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    Hi.

    You only need to add tag order to order:

    <order attribute='attributename' descending='true' />

    or

    <order attribute='attributename' descending='false' />

    Example: Here

  • Community Member Profile Picture
    on at

    I need order attribute on some condition..

    something like below.

     string criteriaCheck = string.Empty;

               if (nyId != Guid.Empty)

               {

                   criteriaCheck = "<condition attribute = 'nyid' operator= 'eq' value = '" + anyId + @"' />";

               }

               else if (!string.IsNullOrWhiteSpace(Code))

               {

                   criteriaCheck = "<condition attribute = 'code' operator= 'eq' value = '" + Code + @"' />";

                   if (Id != Guid.Empty)

                   {

                       criteriaCheck += "<condition attribute = 'rey_brandid' operator= 'eq' value = '" + brandId + @"' />";

                   }

                   if (!string.IsNullOrWhiteSpace(Organization))

                   {

                       criteriaCheck += "<condition attribute = 'organization' operator= 'eq' value = '" + Organization + @"' />";

                   }

               }

    Please help...!

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Shiva,

    Try with this -

                    var FetchXMLRetrieve = String.Empty;
                    var FetchXMLRetrieveCondition = String.Empty;
                    var FetchXMLRetrieveWithCondition = String.Empty;
                    var FetchXMLRetrieveCondition2 = string.Empty;
                    var FetchXMLRetrieveCondition3 = string.Empty;
                    var FetchXMLRetrieveCondition1 = string.Empty;
                    var FetchXMLRetrieveEndTag = string.Empty;
    
                    FetchXMLRetrieve = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                                        <entity name='asisanyuserassociation'>
                                                            <attribute name='asisanyid' />
                                                            <attribute name='amyorganization' />
                                                            <attribute name='aorgid' />
                                                            <attribute name='auserid' />
                                                            <attribute name='aid' />
                                                            <attribute name='aispreferred'/>
                                                            <attribute name='amysysid'/>
                                                            <attribute name='amydid'/>
                                                            <attribute name='ausercode'/>
                                                            <attribute name='asisanyuserassociationtype'/>
                                                            <link-entity name='auser' from='auserid' to='auserid' visible='false' link-type='outer' alias='CDAuserLnk'>
                                                                <attribute name='anamelocation' />
                                                                <attribute name='aid' />
                                                                <attribute name='asapcode'/>
                                                                <attribute name='amyorganization'/>
                                                                <attribute name='aorgid'/>
                                                            <link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAuserorgLnk'> 
                                                                <attribute name='aname' />
                                                                <attribute name='aenum' />
                                                                <attribute name='aalias1' />
                                                            </link-entity>
                                                            </ link-entity>
                                                        <link-entity name='amysys' from='amysysid' to='amysysid' visible='false' link-type='outer' alias='CDAmysysLnk'>
                                                            <attribute name='aid' />
                                                            <attribute name='aserialnumber' />
                                                            <attribute name='aeqmodelid' />
                                                            <attribute name='aeqproductlineid' />
                                                            <attribute name='aorgid' />
                                                        <link-entity name='amysysmodel' from='amysysmodelid' to='aeqmodelid' visible='false' link-type='outer' alias='CDAmysysModelLnkEntity'>
                                                            <attribute name='aid' />
                                                            <attribute name='aname' />
                                                        </link-entity>
                                                        <link-entity name='amysysproductline' from='amysysproductlineid' to='aeqproductlineid' visible='false' link-type='outer' alias='CDAmysysProductLineLnkEntity'>
                                                            <attribute name='aid' />
                                                            <attribute name='aname' />
                                                        </link-entity>
                                                        <link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAmysysorgLnkEntity'>
                                                            <attribute name='aname' />
                                                            <attribute name='aenum' />
                                                            <attribute name='aalias1' />
                                                        </link-entity>
                                                        </link-entity>
                                                        <link-entity name='amyd' from='amydid' to='amydid' visible='false' link-type='outer' alias='CDAmydLnk'>
                                                            <attribute name='aid' />
                                                            <attribute name='asn' />
                                                            <attribute name='aname' />
                                                            <attribute name='amysysid' />
                                                            <attribute name='amydmodelid' />
                                                        <link-entity name='amydmodel' from='amydmodelid' to='amydmodelid' visible='false' link-type='outer' alias='CDAmydModelLnkEntity'>
                                                            <attribute name='apartnumber' />
                                                            <attribute name='aisconnected' />
                                                            <attribute name='aname' />
                                                            <attribute name='aorgid'/>
                                                        <link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAmydModelorgLnkEntity'>
                                                            <attribute name='aname' />
                                                            <attribute name='aenum' />
                                                            <attribute name='aalias1' />
                                                        </link-entity>
                                                      </link-entity>
                                                    </link-entity>
                                                        <link-entity name='aorg' from='aorgid' to='aorgid' visible='false' link-type='outer' alias='CDAorgLnk'>
                                                        <attribute name='aname' />
                                                        <attribute name='aenum' />
                                                        <attribute name='aalias1' />
                                                        </link-entity> 
                                                        <filter type='and'>
                                                        <condition attribute='statecode' operator='eq' value='0'/>
                                                        </filter>
                                                        <filter type='and'>");
    // Assuming that below part will be variable
    //< condition attribute = 'nyid' operator= 'eq' value = '" + anyId + "' /></filter></entity></fetch>"); string criteriaCheck = string.Empty; if (nyId != Guid.Empty) { FetchXMLRetrieveCondition = string.Format(@"<condition attribute = 'nyid' operator= 'eq' value = '" + anyId + "' /></filter></entity></fetch>"); FetchXMLRetrieveWithCondition = FetchXMLRetrieve + FetchXMLRetrieveCondition; } else if (!string.IsNullOrWhiteSpace(Code)) { FetchXMLRetrieveCondition1 = string.Format(@"<condition attribute = 'code' operator= 'eq' value = '" + Code + "' />"); if (Id != Guid.Empty) { FetchXMLRetrieveCondition2 = string.Format(@"<condition attribute = 'rey_brandid' operator= 'eq' value = '" + brandId + "' />"); } if (!string.IsNullOrWhiteSpace(Organization)) { FetchXMLRetrieveCondition3 = string.Format(@"<condition attribute = 'organization' operator= 'eq' value = '" + Organization + "' />"); } FetchXMLRetrieveEndTag = string.Format(@"</filter></entity></fetch>"); FetchXMLRetrieveWithCondition = FetchXMLRetrieve + FetchXMLRetrieveCondition1 + FetchXMLRetrieveCondition3 + FetchXMLRetrieveEndTag; }

    So after concatenate the condition , your final FetchXML will be FetchXMLRetrieveWithCondition 

  • Community Member Profile Picture
    on at

    Hi Goutham, Actually I am looking to place order attribute dynamically based on condition.

    string criteriaCheck = string.Empty;

              if (nyId != Guid.Empty)

              {

                  criteriaCheck = "<condition attribute = 'nyid' operator= 'eq' value = '" + anyId + @"' />";

              }

    The above code worked for my conditions in the same way when nyId is not empty I need to include condition as well as need to sort results based on some other attributes.

    Thanks for the clarififcation

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Shiva ,

    You need to break the fetchXML and concatenate similar way as I mentioned in my code  , so that your order attribute placed correctly after the attributes . Here is sample order attributes highlighted .

     <attribute name="name" />             
                      <order attribute="name" descending="false" />
                      <order attribute="age" descending="false" />
    ---- ---- ---- 
    +

     FetchXMLRetrieveOrder= string.Format(@" < order attribute = 'edm_name' descending = 'false' />");

  • Suggested answer
    Community Member Profile Picture
    on at

    Yes it worked. ! Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans