Get Next business working date in Ms crm 2011
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
namespace MAL.CalRakDate_Workflow
{
public class CalRackDateWorkflow : CodeActivity
{
[Input("Start Date")]
[ReferenceTarget("pact_startdate")]
public InArgument<DateTime> StartDate { get; set; }
[Input("Duration")]
[ReferenceTarget("pact_duration")]
public InArgument<int> NoOfDays { get; set; }
[Input("Resource")]
[ReferenceTarget("equipment")]
public InArgument<EntityReference> Resource { get; set; }
[Output("End Date")]
public OutArgument<DateTime> EndDate { get; set; }
// Comment by Rajeev on Dated : 02/27/2014
//IOrganizationService _service;
//DateTime finalDate = new DateTime();
DateTime finalDate;
protected override void Execute(CodeActivityContext context)
{
finalDate = new DateTime();
// if req. the tracing service, uncomment the following line.
ITracingService tracingservice = context.GetExtension<ITracingService>();
// create the Context
IWorkflowContext wfContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
//_service = serviceFactory.CreateOrganizationService(wfContext.InitiatingUserId); - Comment by Rajeev
IOrganizationService _service = serviceFactory.CreateOrganizationService(wfContext.InitiatingUserId);
DateTime startDt = this.StartDate.Get(context);
int noOfHours = this.NoOfDays.Get(context);
Guid Resource = this.Resource.Get(context).Id;
decimal noOfDays = noOfHours / 24;
int finalcount = 0;
try
{
QueryScheduleRequest scheduleRequest = new QueryScheduleRequest();
scheduleRequest.ResourceId = Resource;
scheduleRequest.Start = startDt;
//scheduleRequest.End = startDt.AddDays(7); - Comment by Rajeev
scheduleRequest.End = startDt.AddDays(20);
scheduleRequest.TimeCodes = new TimeCode[] { TimeCode.Available };
QueryScheduleResponse scheduleResponse = (QueryScheduleResponse)_service.Execute(scheduleRequest);
for (int i = 1; i < 100; i++)
{
if (finalcount == (int)Math.Round(noOfDays, 0))
{
break;
}
// Comment by Rajeev on Dated 02/27/2014
//else if (ProcessEndDate(scheduleResponse, startDt.AddDays(i), context))
//{
// finalcount++;
//}
else if (ProcessEndDate(scheduleResponse, startDt.AddDays(i), _service))
{
finalcount++;
}
}
this.EndDate.Set(context, finalDate);
}
catch (Exception ex)
{
}
}
/// <summary>
/// Return true if it is Business day for user
/// </summary>
/// <param name="scheduleResponse">scheduleResponse</param>
/// <param name="dt">Datetime</param>
/// <param name="context">context</param>
/// <returns>Return true if it is Business day else false</returns>
// **************************************************************************************************************
// Comment by Rajeev on dated 02/27/2014
//public bool ProcessEndDate(QueryScheduleResponse scheduleResponse, DateTime dt, CodeActivityContext context)
//{
// bool check = false;
// foreach (TimeInfo ti in scheduleResponse.TimeInfos)
// {
// if (dt.Date == ti.Start.Value.Date)
// {
// if (!CheckHolidays(dt))
// {
// finalDate = dt;
// check = true;
// }
// }
// }
// return check;
//}
// ***************************************************************************************************************
public bool ProcessEndDate(QueryScheduleResponse scheduleResponse, DateTime dt, IOrganizationService _service)
{
bool check = false;
foreach (TimeInfo ti in scheduleResponse.TimeInfos)
{
if (dt.Date == ti.Start.Value.Date)
{
if (!CheckHolidays(dt, _service))
{
finalDate = dt;
check = true;
}
}
}
return check;
}
/// <summary>
/// Check Holiday in calender
/// </summary>
/// <param name="date">Datetime</param>
/// <returns></returns>
// Comment by Rajeev on Dated : 02/27/2014
// **********************************************************************************************************************************
//public bool CheckHolidays(DateTime date)
//{
// QueryExpression query = new QueryExpression("calendar");
// query.ColumnSet = new ColumnSet(true);
// ConditionExpression condition = new ConditionExpression();
// condition.AttributeName = "name";
// condition.Operator = ConditionOperator.Equal;
// condition.Values.Add("Business Closure Calendar");
// query.Criteria.Conditions.Add(condition);
// EntityCollection calendars = _service.RetrieveMultiple(query);
// EntityCollection calendarrule = calendars[0].GetAttributeValue<EntityCollection>("calendarrules");
// return calendarrule.Entities
// .Where(e => ((DateTime)e["starttime"]).Date == date.Date).Any();
//}
// ***********************************************************************************************************************************
public bool CheckHolidays(DateTime date, IOrganizationService _service)
{
QueryExpression query = new QueryExpression("calendar");
query.ColumnSet = new ColumnSet(true);
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "name";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add("Business Closure Calendar");
query.Criteria.Conditions.Add(condition);
EntityCollection calendars = _service.RetrieveMultiple(query);
EntityCollection calendarrule = calendars[0].GetAttributeValue<EntityCollection>("calendarrules");
return calendarrule.Entities.Where(e => ((DateTime)e["starttime"]).Date == date.Date).Any();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
namespace MAL.CalRakDate_Workflow
{
public class CalRackDateWorkflow : CodeActivity
{
[Input("Start Date")]
[ReferenceTarget("pact_startdate")]
public InArgument<DateTime> StartDate { get; set; }
[Input("Duration")]
[ReferenceTarget("pact_duration")]
public InArgument<int> NoOfDays { get; set; }
[Input("Resource")]
[ReferenceTarget("equipment")]
public InArgument<EntityReference> Resource { get; set; }
[Output("End Date")]
public OutArgument<DateTime> EndDate { get; set; }
// Comment by Rajeev on Dated : 02/27/2014
//IOrganizationService _service;
//DateTime finalDate = new DateTime();
DateTime finalDate;
protected override void Execute(CodeActivityContext context)
{
finalDate = new DateTime();
// if req. the tracing service, uncomment the following line.
ITracingService tracingservice = context.GetExtension<ITracingService>();
// create the Context
IWorkflowContext wfContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
//_service = serviceFactory.CreateOrganizationService(wfContext.InitiatingUserId); - Comment by Rajeev
IOrganizationService _service = serviceFactory.CreateOrganizationService(wfContext.InitiatingUserId);
DateTime startDt = this.StartDate.Get(context);
int noOfHours = this.NoOfDays.Get(context);
Guid Resource = this.Resource.Get(context).Id;
decimal noOfDays = noOfHours / 24;
int finalcount = 0;
try
{
QueryScheduleRequest scheduleRequest = new QueryScheduleRequest();
scheduleRequest.ResourceId = Resource;
scheduleRequest.Start = startDt;
//scheduleRequest.End = startDt.AddDays(7); - Comment by Rajeev
scheduleRequest.End = startDt.AddDays(20);
scheduleRequest.TimeCodes = new TimeCode[] { TimeCode.Available };
QueryScheduleResponse scheduleResponse = (QueryScheduleResponse)_service.Execute(scheduleRequest);
for (int i = 1; i < 100; i++)
{
if (finalcount == (int)Math.Round(noOfDays, 0))
{
break;
}
// Comment by Rajeev on Dated 02/27/2014
//else if (ProcessEndDate(scheduleResponse, startDt.AddDays(i), context))
//{
// finalcount++;
//}
else if (ProcessEndDate(scheduleResponse, startDt.AddDays(i), _service))
{
finalcount++;
}
}
this.EndDate.Set(context, finalDate);
}
catch (Exception ex)
{
}
}
/// <summary>
/// Return true if it is Business day for user
/// </summary>
/// <param name="scheduleResponse">scheduleResponse</param>
/// <param name="dt">Datetime</param>
/// <param name="context">context</param>
/// <returns>Return true if it is Business day else false</returns>
// **************************************************************************************************************
// Comment by Rajeev on dated 02/27/2014
//public bool ProcessEndDate(QueryScheduleResponse scheduleResponse, DateTime dt, CodeActivityContext context)
//{
// bool check = false;
// foreach (TimeInfo ti in scheduleResponse.TimeInfos)
// {
// if (dt.Date == ti.Start.Value.Date)
// {
// if (!CheckHolidays(dt))
// {
// finalDate = dt;
// check = true;
// }
// }
// }
// return check;
//}
// ***************************************************************************************************************
public bool ProcessEndDate(QueryScheduleResponse scheduleResponse, DateTime dt, IOrganizationService _service)
{
bool check = false;
foreach (TimeInfo ti in scheduleResponse.TimeInfos)
{
if (dt.Date == ti.Start.Value.Date)
{
if (!CheckHolidays(dt, _service))
{
finalDate = dt;
check = true;
}
}
}
return check;
}
/// <summary>
/// Check Holiday in calender
/// </summary>
/// <param name="date">Datetime</param>
/// <returns></returns>
// Comment by Rajeev on Dated : 02/27/2014
// **********************************************************************************************************************************
//public bool CheckHolidays(DateTime date)
//{
// QueryExpression query = new QueryExpression("calendar");
// query.ColumnSet = new ColumnSet(true);
// ConditionExpression condition = new ConditionExpression();
// condition.AttributeName = "name";
// condition.Operator = ConditionOperator.Equal;
// condition.Values.Add("Business Closure Calendar");
// query.Criteria.Conditions.Add(condition);
// EntityCollection calendars = _service.RetrieveMultiple(query);
// EntityCollection calendarrule = calendars[0].GetAttributeValue<EntityCollection>("calendarrules");
// return calendarrule.Entities
// .Where(e => ((DateTime)e["starttime"]).Date == date.Date).Any();
//}
// ***********************************************************************************************************************************
public bool CheckHolidays(DateTime date, IOrganizationService _service)
{
QueryExpression query = new QueryExpression("calendar");
query.ColumnSet = new ColumnSet(true);
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "name";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add("Business Closure Calendar");
query.Criteria.Conditions.Add(condition);
EntityCollection calendars = _service.RetrieveMultiple(query);
EntityCollection calendarrule = calendars[0].GetAttributeValue<EntityCollection>("calendarrules");
return calendarrule.Entities.Where(e => ((DateTime)e["starttime"]).Date == date.Date).Any();
}
}
}
No comments:
Post a Comment