protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } DateTime dateToCheck = DateToCheck.Get(context); bool evaluateAsUserLocal = EvaluateAsUserLocal.Get(context); Entity calendar = null; EntityReference holidayClosureCalendar = HolidayClosureCalendar.Get(context); if (holidayClosureCalendar != null) { calendar = localContext.OrganizationService.Retrieve("calendar", holidayClosureCalendar.Id, new ColumnSet(true)); } if (evaluateAsUserLocal) { int?timeZoneCode = GetLocalTime.RetrieveTimeZoneCode(localContext.OrganizationService); dateToCheck = GetLocalTime.RetrieveLocalTimeFromUtcTime(dateToCheck, timeZoneCode, localContext.OrganizationService); } var result = dateToCheck.IsBusinessDay(calendar); ValidBusinessDay.Set(context, result); }
protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } DateTime originalDate = OriginalDate.Get(context); int businessDaysToAdd = BusinessDaysToAdd.Get(context); EntityReference holidaySchedule = HolidayClosureCalendar.Get(context); Entity calendar = null; if (holidaySchedule != null) { calendar = localContext.OrganizationService.Retrieve("calendar", holidaySchedule.Id, new ColumnSet(true)); } DateTime tempDate = originalDate; if (businessDaysToAdd > 0) { while (businessDaysToAdd > 0) { tempDate = tempDate.AddDays(1); if (tempDate.IsBusinessDay(calendar)) { // Only decrease the days to add if the day we've just added counts as a business day businessDaysToAdd--; } } } else if (businessDaysToAdd < 0) { while (businessDaysToAdd < 0) { tempDate = tempDate.AddDays(-1); if (tempDate.IsBusinessDay(calendar)) { // Only increase the days to add if the day we've just added counts as a business day businessDaysToAdd++; } } } DateTime updatedDate = tempDate; UpdatedDate.Set(context, updatedDate); }
protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } var dateToCheck = StartDate.Get(context); dateToCheck = new DateTime(dateToCheck.Year, dateToCheck.Month, dateToCheck.Day, 0, 0, 0); var endDate = EndDate.Get(context); endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 0, 0, 0); Entity calendar = null; EntityReference holidayClosureCalendar = HolidayClosureCalendar.Get(context); if (holidayClosureCalendar != null) { calendar = localContext.OrganizationService.Retrieve("calendar", holidayClosureCalendar.Id, new ColumnSet(true)); } var businessDays = 0; while (dateToCheck <= endDate) { if (dateToCheck.IsBusinessDay(calendar)) { businessDays++; } dateToCheck = dateToCheck.AddDays(1); } NumberOfBusinessDays.Set(context, businessDays); }
protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } var startDate = StartingDate.Get(context); startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, startDate.Minute, 0); var endDate = EndingDate.Get(context); endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute, 0); if (startDate == endDate) { MinutesDifference.Set(context, 0); return; } Entity calendar = null; EntityReference holidayClosureCalendar = HolidayClosureCalendar.Get(context); if (holidayClosureCalendar != null) { calendar = localContext.OrganizationService.Retrieve("calendar", holidayClosureCalendar.Id, new ColumnSet(true)); } var businessMinutes = 0; if (endDate > startDate) { while (startDate < endDate) { if (startDate.IsBusinessMinute(calendar)) { businessMinutes++; } startDate = startDate.AddMinutes(1); } } else { while (startDate > endDate) { if (startDate.IsBusinessMinute(calendar)) { businessMinutes--; } startDate = startDate.AddMinutes(-1); } } MinutesDifference.Set(context, businessMinutes); }
protected override void Execute(CodeActivityContext executionContext) { ITracingService tracer = executionContext.GetExtension <ITracingService>(); IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { DateTime dateToCheck = DateToCheck.Get(executionContext); bool evaluateAsUserLocal = EvaluateAsUserLocal.Get(executionContext); if (evaluateAsUserLocal) { GetLocalTime glt = new GetLocalTime(); int? timeZoneCode = glt.RetrieveTimeZoneCode(service); dateToCheck = glt.RetrieveLocalTimeFromUtcTime(dateToCheck, timeZoneCode, service); } EntityReference holidaySchedule = HolidayClosureCalendar.Get(executionContext); bool validBusinessDay = dateToCheck.DayOfWeek != DayOfWeek.Saturday || dateToCheck.DayOfWeek == DayOfWeek.Sunday; if (!validBusinessDay) { ValidBusinessDay.Set(executionContext, false); return; } if (holidaySchedule != null) { Entity calendar = service.Retrieve("calendar", holidaySchedule.Id, new ColumnSet(true)); if (calendar == null) { return; } EntityCollection calendarRules = calendar.GetAttributeValue <EntityCollection>("calendarrules"); foreach (Entity calendarRule in calendarRules.Entities) { //Date is not stored as UTC DateTime startTime = calendarRule.GetAttributeValue <DateTime>("starttime"); //Not same date if (!startTime.Date.Equals(dateToCheck.Date)) { continue; } //Not full day event if (startTime.Subtract(startTime.TimeOfDay) != startTime || calendarRule.GetAttributeValue <int>("duration") != 1440) { continue; } ValidBusinessDay.Set(executionContext, false); return; } } ValidBusinessDay.Set(executionContext, true); } catch (Exception ex) { tracer.Trace("Exception: {0}", ex.ToString()); } }
/// <summary> /// Executes the workflow activity. /// </summary> /// <param name="executionContext">The execution context.</param> /// protected override void Execute(CodeActivityContext executionContext) { ITracingService tracer = executionContext.GetExtension <ITracingService>(); IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { DateTime originalDate = OriginalDate.Get(executionContext); int businessDaysToAdd = BusinessDaysToAdd.Get(executionContext); EntityReference holidaySchedule = HolidayClosureCalendar.Get(executionContext); Boolean ishighLevel = IsHighLevel.Get(executionContext); Int32 decreaseHours = DecreaseHours.Get(executionContext); Entity calendar = null; EntityCollection calendarRules = null; if (holidaySchedule != null) { calendar = service.Retrieve("calendar", holidaySchedule.Id, new ColumnSet(true)); if (calendar != null) { calendarRules = calendar.GetAttributeValue <EntityCollection>("calendarrules"); } } DateTime tempDate = originalDate; DateTime tempDateHours = originalDate; /* * * Test segment */ if (ishighLevel) { Int32 DecreaseHoursNegative = decreaseHours * -1; tempDateHours = tempDateHours.AddHours(DecreaseHoursNegative); if (tempDateHours.Day != tempDate.Day) { businessDaysToAdd = -1; tempDate = tempDate.AddHours(DecreaseHoursNegative); } } /****************/ if (businessDaysToAdd > 0) { while (businessDaysToAdd > 0) { tempDate = tempDate.AddDays(1); if (tempDate.DayOfWeek == DayOfWeek.Sunday || tempDate.DayOfWeek == DayOfWeek.Saturday) { continue; } if (calendar == null) { businessDaysToAdd--; continue; } bool isHoliday = false; foreach (Entity calendarRule in calendarRules.Entities) { DateTime startTime = calendarRule.GetAttributeValue <DateTime>("starttime"); //Not same date if (!startTime.Date.Equals(tempDate.Date)) { continue; } //Not full day event if (startTime.Subtract(startTime.TimeOfDay) != startTime || calendarRule.GetAttributeValue <int>("duration") != 1440) { continue; } isHoliday = true; break; } if (!isHoliday) { businessDaysToAdd--; } } } else if (businessDaysToAdd < 0) { while (businessDaysToAdd < 0) { tempDate = tempDate.AddDays(-1); if (tempDate.DayOfWeek == DayOfWeek.Sunday || tempDate.DayOfWeek == DayOfWeek.Saturday) { continue; } if (calendar == null) { businessDaysToAdd++; continue; } bool isHoliday = false; foreach (Entity calendarRule in calendarRules.Entities) { DateTime startTime = calendarRule.GetAttributeValue <DateTime>("starttime"); //Not same date if (!startTime.Date.Equals(tempDate.Date)) { continue; } //Not full day event if (startTime.Subtract(startTime.TimeOfDay) != startTime || calendarRule.GetAttributeValue <int>("duration") != 1440) { continue; } isHoliday = true; break; } if (!isHoliday) { businessDaysToAdd++; } } } DateTime updatedDate = tempDate; UpdatedDate.Set(executionContext, updatedDate); } catch (Exception ex) { tracer.Trace("Exception: {0}", ex.ToString()); } }