Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

FetchXML aggregation for two option fields

Posted on by 10

Good Evening,

I'm trying to aggregate a two option field with the following test FetchXML:

   
      
      

But get the following error:

Aggregate MAX or MIN is not supported for attribute of type bit.

Essentially I need to find if any contact has 'field1' set to 'Yes'.  Is it possible to aggregate a two option field?

  • JV_FLA Profile Picture
    JV_FLA 20 on at
    RE: FetchXML aggregation for two option fields

    Wahaj, in your "alternated approach" why do you even need aggregation at all in the fetchXML for this? Is that really needed or just forgot to delete that out?

    <fetch distinct='false' mapping='logical' aggregate='true' top='1'>

      <entity name="contact">

         <attribute name="field1"/>

         <filter type="and">

           <condition attribute="field1" operator="eq" value="1" />

       </filter>

      </entity>  

    </fetch>

  • Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: FetchXML aggregation for two option fields

    To make it easier for other readers, If any of the answers helped, please mark it as verified.

    (Select 'Yes' under 'Did this answer your question?')

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: FetchXML aggregation for two option fields

    Hi,

    I believe you have to do multiple queries for each field and then implement your logic.

    For example:

    • Query field1 where value = 1 and store the result in a variable (havingField1 = true).
    • Query field2 where value = 1 and store the result in a variable (havingField2 = true).
    • Query field3 where value = 1 and store the result in a variable (havingField3 = true).
    • The implement your logic like if (havingField1 && havingField2 and havingField3).

    Otherwise, please look into Clofy's approach.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: FetchXML aggregation for two option fields

    Hi Tom,

    SQL is now supported(and MAX function) to query CDS data(now it is renamed to Dataverse), try the new preview feature and check whether it could work for you:

    https://docs.microsoft.com/en-us/powerapps/developer/data-platform/dataverse-sql-query

    https://powerobjects.com/dynamics-365/use-sql-to-query-data-from-cds-and-dynamics-365-ce/

    Regards,

    Clofly

  • Tom13212 Profile Picture
    Tom13212 10 on at
    RE: FetchXML aggregation for two option fields

    Hi Wahaj,

    Thanks for your response, what would be the best way to structure a query that was aggregating across multiple fields rather than just one?

    Essentially I'm trying to create a query to determine whether any related contact has a field set to true for a number of fields, in SQL the equivalent query would be:

    SELECT id, MAX(field1), MAX(field2), MAX(field3), MAX(field4)
    FROM contacts
    GROUP BY id
    WHERE ...

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: FetchXML aggregation for two option fields

    Hi,

    Thank you for your query.

    As you can see by the error it is not possible Aggregate on bit (2 optionset) fields.

    However you can use DISTINCT operator.

    Here is the example FetchXML:

      
        
      
    

    Please note, distinct="true" added in the FechXML and it should return 2 records (at MAX).

    An alternated approach to your scenario is:

    • Retrieve Records where field1 is true.
    • Set Top to 1, so we get first record where field1 is true.
    • If the result count is greater than 0, you have your answer.

    For instance here is the example FetchXML:

       
          
          
            
        
          
    

    If there are any results returned from above query, we will get to know if there are any records where field1 is set to true.

    Here is example C# code for your reference:

    string qry = @"
                       
                          
                          
                            
                        
                          
                    ";
    
                FetchExpression fetchExpression = new FetchExpression(qry);
    
                var results = svc.RetrieveMultiple(fetchExpression);
                
                if (results.Entities.Count==1)
                {
                    // There are records where field1=true
                    return true;
                }

    Let me know if you need further clarification.

    Best,

    Wahaj

    (if this helps, please mark it as verified)

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans