Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Convert FetchExpression to QueryExpression in Plugin

Posted on by 370

I have a plugin that get a query in the form of a FetchExpression and I need to convert it to a QueryExpression so I can manipulate it in a number of different ways. From what I have read this should be relatively simple based on https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/org-service/sample-convert-queries-fetch-queryexpression

However I have not been able to get it to work. Code below:

private QueryExpression GetQuery(IPluginExecutionContext context, IServiceProvider serviceProvider, ITracingService tracing)
        {
            if (context.InputParameters["Query"] is QueryExpression)
            {
                return context.InputParameters["Query"] as QueryExpression;
            }
            else if (context.InputParameters["Query"] is FetchExpression)
            {
                FetchExpression fetch = context.InputParameters["Query"] as FetchExpression;
                tracing.Trace("XML: {0}",fetch.Query);
                try
                {
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    FetchXmlToQueryExpressionRequest conversionRequest = new FetchXmlToQueryExpressionRequest() { FetchXml = fetch.Query };
                    FetchXmlToQueryExpressionResponse conversionResponse = (service.Execute(conversionRequest) as FetchXmlToQueryExpressionResponse);
                    
                    return conversionResponse.Query;
                }
                catch (Exception ee)
                {
                    throw ee;
                }
            }
            else return null;
        } 


The problem I encounter is that the resulting queryexpression has a query.Criteria.Conditions.Count of 0. Even thought his is the Fetch XML that is being passed in and it clearly has conditions (see below). 

<fetch count="4" mapping="logical" output-format="xml-platform" version="1.0" returntotalrecordcount="true" page="1" no-lock="false" distinct="false">
<entity name="activitypointer">
<attribute name="subject" />
<attribute name="activitytypecode" />
<attribute name="statecode" />
<attribute name="modifiedon" />
<attribute name="description" />
<attribute name="scheduledend" />
<attribute name="actualend" />
<order descending="true" attribute="modifiedon" />
<order descending="false" attribute="statecode" />
<filter type="and">
<condition attribute="activityid" operator="null" />
<condition value="%Case%" attribute="description" operator="like" />
</filter>
<attribute name="regardingobjectid" />
<attribute name="ownerid" />
<attribute name="activityid" />
<link-entity name="incident" alias="bb" to="regardingobjectid" from="incidentid">
<filter type="and">
<condition value="7eed7742-c3c8-e811-a964-000d3a3ac063" attribute="incidentid" operator="eq" uitype="incident" />
</filter>
</link-entity><filter type="and">
<filter type="and">
<condition value="4406" attribute="activitytypecode" operator="ne" />
</filter>
<filter type="and"> <condition value="4251" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4209" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4220" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4206" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4204" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4208" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4214" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4207" attribute="activitytypecode" operator="ne" /> </filter> <filter type="and"> <condition value="4211" attribute="activitytypecode" operator="ne" /> </filter> </filter> </entity> </fetch>


Anyone have any ideas what I am doing wrong?

*This post is locked for comments

  • ArdantHammer Profile Picture
    ArdantHammer 370 on at
    RE: Convert FetchExpression to QueryExpression in Plugin

    Ravi,

    Thank I think I will just have to adapt the code to handle the fetch or the query dependent on which format it comes. From thank you for helping me figure it out.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Convert FetchExpression to QueryExpression in Plugin

    Ohh Got it. Considering that both interface are different, it is possible that Microsoft is handling the retrieval slight differently. Did you check if there is anything in the plugin properties which can identify if it is classic or unified. If you can then you could have a code block specific to the interface.

    Hope this helps.

  • ArdantHammer Profile Picture
    ArdantHammer 370 on at
    RE: Convert FetchExpression to QueryExpression in Plugin

    I am running this in a plugin. It runs based on the retrievemultiple(I know unsupported and not ideal for performance). When it is triggered on the unified interface it gives me the query as a fetchexpression but when it is called from the classic interface it passes me a queryexpression. Does that help clarify?

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Convert FetchExpression to QueryExpression in Plugin

    I don't even know if we can run the same code specific to the interface i.e. for classic interface and unified interface. Can we?

  • ArdantHammer Profile Picture
    ArdantHammer 370 on at
    RE: Convert FetchExpression to QueryExpression in Plugin

    Ravi,

    Hmm, you are correct! When I use the check on conversionResponse.Query.Criteria.Filters.Count it comes back correctly as 2. However when I pull the conversionResponse.Query.Criteria.Conditions.Count I get 0. I guess this has to do with a difference in the way the query is structured? What I find strange is the reason I am working on this is that when I do this in the classic interface Dynamics gives me the same query in the filterExpression and it has a Criteria.Conditions.Count  of 2, but in the unified interface it feeds me the same query as a fetchExpression converted to filterExpression has that same count is 0. Any thoughts on why these might be different?

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Convert FetchExpression to QueryExpression in Plugin

    Hi,

    I checked this and all your conditions are added under the filter which is available on condition.

    conversionResponse.Query.Criteria.Filters.Count

    6724.FILTERS.png

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans