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