Wednesday, May 25, 2016

Retrieve task 2 days after and before today's date using Query expression in CRM 2015

  QueryExpression queryExp = new QueryExpression();
                queryExp.EntityName = "task";
                queryExp.ColumnSet = new ColumnSet();
                queryExp.ColumnSet.AddColumns("activityid", "statecode", "scheduledend", "aces_notificationdate");

                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "scheduledend";
                condition1.Operator = ConditionOperator.On;
                condition1.Values.Add(DateTime.UtcNow.AddDays(2));
          
                ConditionExpression condition2 = new ConditionExpression();
                condition2.AttributeName = "statecode";
                condition2.Values.Add(0);//condition to retrieve cases associated with Master Case

                ConditionExpression condition3 = new ConditionExpression();
                condition3.AttributeName = "scheduledend";
                condition3.Operator = ConditionOperator.On;
                condition3.Values.Add(DateTime.UtcNow.AddDays(-2));
           
                    //FilterExpression filterExpression = new FilterExpression();
                    //filterExpression.AddCondition(condition1);
                    //filterExpression.AddCondition(condition2);
                    //filterExpression.AddCondition(condition3);
                    //filterExpression.FilterOperator = LogicalOperator.And;


            FilterExpression filterExpression = new FilterExpression();
            FilterExpression filterExpression1 = new FilterExpression();
            FilterExpression filterExpression2 = new FilterExpression();

            filterExpression1.AddCondition(condition1);
            filterExpression1.AddCondition(condition2);

            filterExpression2.AddCondition(condition2);
            filterExpression2.AddCondition(condition3);

            filterExpression.FilterOperator = LogicalOperator.Or;
            filterExpression1.FilterOperator = LogicalOperator.And;
            filterExpression2.FilterOperator = LogicalOperator.And;
            filterExpression.AddFilter(filterExpression1);
            filterExpression.AddFilter(filterExpression2);





            queryExp.Criteria = filterExpression;

No comments:

Post a Comment