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)); } DateTime originalDate = OriginalDate.Get(context); int yearsToAdd = YearsToAdd.Get(context); DateTime updatedDate = originalDate.AddYears(yearsToAdd); UpdatedDate.Set(context, updatedDate); }
protected override void Execute(CodeActivityContext executionContext) { ITracingService tracer = executionContext.GetExtension <ITracingService>(); try { DateTime originalDate = OriginalDate.Get(executionContext); int hoursToAdd = HoursToAdd.Get(executionContext); DateTime updatedDate = originalDate.AddHours(hoursToAdd); UpdatedDate.Set(executionContext, updatedDate); } 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()); } }