Tuesday, January 12, 2021

Dynamic CRM RetrieveMultiple fetching Condition expression

 

I observe that RetrieveMultiple build query different for Classic UI and UCI and to my surprise when we use default sub grid with displaying only related records, it comes with a difference in query getting generated. 

Here, I have used to code to fetch condition expression that I was interested in and use it further to my business logic


 

if (context.InputParameters.Contains("Query"))

{

if (context.InputParameters["Query"] is FetchExpression)

{  

    // It seems UCI build and send Fetch Expression

    FetchExpression fetchExpression = context.InputParameters["Query"] as FetchExpression;

    FetchXmlToQueryExpressionRequest fetchXmlToQueryExpressionRequest = new FetchXmlToQueryExpressionRequest()

    {

        FetchXml = fetchExpression.Query

    };

    FetchXmlToQueryExpressionResponse fetchXmlToQueryExpressionResponse = (service.Execute(fetchXmlToQueryExpressionRequest) as FetchXmlToQueryExpressionResponse);

    pluginQueryExp = fetchXmlToQueryExpressionResponse.Query;                                   

}

else

if (context.InputParameters["Query"] is QueryExpression)

{

    // Classic UI build and send Query Expression

    pluginQueryExp = ((QueryExpression)context.InputParameters["Query"]);                

}

 

if (pluginQueryExp.LinkEntities.Count > 0)

{

    if (pluginQueryExp.LinkEntities[0].LinkCriteria.Conditions.Count > 0)

    {

        accountId = new Guid(pluginQueryExp.LinkEntities[0].LinkCriteria.Conditions[0].Values[0].ToString());

    }

}

else if (pluginQueryExp.Criteria.Conditions[0].Values.Count > 0)

    accountId = new Guid(pluginQueryExp.Criteria.Conditions[0].Values[0].ToString());

}