CRM 2015 FetchXML Plugin , how to get query conditions

You can use this method to get parameters from filter conditions. I used it to get parametric fields to calculate price on dummy calcprice entity.

 public void Execute(IServiceProvider serviceProvider)
        {
            var organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            var orgService = organizationServiceFactory.CreateOrganizationService(pluginContext.UserId);

            if (
            //    pluginContext.InputParameters["Queue"] is FetchExpression &&
                 pluginContext.MessageName.Equals("RetrieveMultiple", StringComparison.OrdinalIgnoreCase)
                && pluginContext.PrimaryEntityName.Equals("new_zcalcprice", StringComparison.OrdinalIgnoreCase)
             //   && pluginContext.InputParameters.ContainsKey("Query")
                )
            {
                
                var fetchExpression = (FetchExpression)pluginContext.InputParameters["Query"];
         
                FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
                req.FetchXml = fetchExpression.Query;
                var resp = (FetchXmlToQueryExpressionResponse)orgService.Execute(req);
                QueryExpression queryExpression = resp.Query;
                Entity calcpriceentity = new Entity("new_zcalcprice");
                calcpriceentity["new_name"] = Guid.NewGuid().ToString();

                WhoAmIRequest userRequest = new WhoAmIRequest();
                WhoAmIResponse user = (WhoAmIResponse)orgService.Execute(userRequest);
                Guid myUserId = user.UserId;
                //  calcpriceentity["ownerid"] = myUserId;

                //AssignRequest assignRequest = new AssignRequest
                //{
                //    Assignee = new EntityReference("systemuser", myUserId),
                //    Target = new EntityReference("account", myUserId)
                //};
                //orgService.Execute(assignRequest);


                //calcpriceentity.Attributes.Add("ownerid", myUserId);

               
                foreach (var condition in queryExpression.Criteria.Filters[0].Conditions)
                {
                    
                    if (condition.AttributeName.Equals("new_product"))
                    {
                        calcpriceentity["new_product"] = new EntityReference("product", (Guid)condition.Values[0]);
                    }
                        
                    if (condition.AttributeName.Equals("new_pricelevelid"))
                    {
                        calcpriceentity["new_pricelevelid"] = (Guid)condition.Values[0];
                    }

                    if (condition.AttributeName.Equals("new_priceperunit"))
                    {
                        calcpriceentity["new_priceperunit"] = new Money(Convert.ToDecimal(condition.Values[0]));
                    }

                    if (condition.AttributeName.Equals("new_quantity"))
                    {
                        calcpriceentity["new_quantity"] = Convert.ToDecimal(condition.Values[0]);
                    }

                    if (condition.AttributeName.Equals("new_baseamount"))
                    {
                        calcpriceentity["new_baseamount"] = new Money(Convert.ToDecimal(condition.Values[0]));
                    }

                    if (condition.AttributeName.Equals("new_extendedamount"))
                    {
                        calcpriceentity["new_extendedamount"] = new Money(Convert.ToDecimal(condition.Values[0]));
                    }

                    if (condition.AttributeName.Equals("new_tax"))
                    {
                        calcpriceentity["new_tax"] = new Money(Convert.ToDecimal(condition.Values[0]));
                    }
                    if (condition.AttributeName.Equals("new_volumediscountamount"))
                    {
                        calcpriceentity["new_volumediscountamount"] = new Money(Convert.ToDecimal(condition.Values[0]));
                    }

                    if (condition.AttributeName.Equals("new_manualdiscountamount"))
                    {
                        calcpriceentity["new_manualdiscountamount"] = new Money(Convert.ToDecimal(condition.Values[0]));
                    }

                }



                orgService.Create(calcpriceentity);

            }
        }

Leave a Reply

Your email address will not be published.