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 dateToUse = DateToUse.Get(context); bool evaluateAsUserLocal = EvaluateAsUserLocal.Get(context); if (evaluateAsUserLocal) { int?timeZoneCode = GetLocalTime.RetrieveTimeZoneCode(localContext.OrganizationService); dateToUse = GetLocalTime.RetrieveLocalTimeFromUtcTime(dateToUse, timeZoneCode, localContext.OrganizationService); } int diff = dateToUse.DayOfWeek - DayOfWeek.Sunday; if (diff < 0) { diff += 7; } DateTime weekStartDate = dateToUse.AddDays(-1 * diff).Date; weekStartDate = new DateTime(weekStartDate.Year, weekStartDate.Month, weekStartDate.Day, 0, 0, 0); DateTime weekEndDate = weekStartDate.AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999); WeekStartDate.Set(context, weekStartDate); WeekEndDate.Set(context, weekEndDate); }
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 dateToUse = DateToUse.Get(executionContext); bool evaluateAsUserLocal = EvaluateAsUserLocal.Get(executionContext); if (evaluateAsUserLocal) { GetLocalTime glt = new GetLocalTime(); int? timeZoneCode = glt.RetrieveTimeZoneCode(service); dateToUse = glt.RetrieveLocalTimeFromUtcTime(dateToUse, timeZoneCode, service); } int diff = dateToUse.DayOfWeek - DayOfWeek.Sunday; if (diff < 0) { diff += 7; } DateTime weekStartDate = dateToUse.AddDays(-1 * diff).Date; weekStartDate = new DateTime(weekStartDate.Year, weekStartDate.Month, weekStartDate.Day, 0, 0, 0); DateTime weekEndDate = weekStartDate.AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999); WeekStartDate.Set(executionContext, weekStartDate); WeekEndDate.Set(executionContext, weekEndDate); } catch (Exception ex) { tracer.Trace("Exception: {0}", ex.ToString()); } }