Wednesday 8 August 2012

On or before x number of days


Currently in CRM filter criteria doesn't provide the facility to view records which are are valid on or before 3 days.

IOrganizationService crmService = localContext.OrganizationService;

            object query = localContext.PluginExecutionContext.InputParameters["Query"];
            if (query.GetType() == typeof(QueryExpression))
            {
                QueryExpression qe = (QueryExpression)localContext.PluginExecutionContext.InputParameters["Query"];

                FilterExpression fe = qe.Criteria;

                var filterAttributeCondition = (from c in fe.Conditions
                                                where c.AttributeName == "new_retrievemultiplefilter"
                                                select c).FirstOrDefault();

                if (filterAttributeCondition != null && (string)filterAttributeCondition.Values[0] == "records on or before x number of days")
                {

                    fe.Conditions.Remove(filterAttributeCondition);

                    var xNumberOfDaysCondition = (from c in fe.Conditions
                                                  where c.AttributeName == "new_xnumberofdays"
                                                  select c).FirstOrDefault();

                    if (xNumberOfDaysCondition != null)
                    {
                        int xNumberOfDays = xNumberOfDaysCondition.Values.Count > 0 ? (int)xNumberOfDaysCondition.Values[0] : 0;

                        fe.Conditions.Remove(xNumberOfDaysCondition);

                        DateTime dateOnXnumberDaysBefore = DateTime.Now.AddDays(-xNumberOfDays) ;


                        fe.Conditions.Add(
                            new ConditionExpression(
                                "attributename",
                                ConditionOperator.OnOrBefore,
                                new object[]{dateOnXnumberDaysBefore})
                                );
                    }
                }
            }

No comments:

Post a Comment