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)

Convert FetchExpression to QueryExpression in Plugin

(0) ShareShare
ReportReport
Posted on by 372

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

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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

  • ArdantHammer Profile Picture
    372 on at

    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?

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    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
    372 on at

    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
    55,410 Moderator on at

    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
    372 on at

    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.

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