Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Order Type in Fetch XML

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

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Order Type in Fetch XML

    Yes it worked. ! Thanks

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Order Type in Fetch XML

    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' />");

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Order Type in Fetch XML

    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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Order Type in Fetch XML

    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
    Community Member Microsoft Employee on at
    RE: Order Type in Fetch XML

    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
    Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Order Type in Fetch XML

    Hi.

    You only need to add tag order to order:

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

    or

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

    Example: Here

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans