Custom Workflow To get Datetime for the specified day of the week


One of my client ask me to develop a custom workflow for Following Date Manipulation functions

Get the day of the week from a datetime field

Get the date only part of a date time field to a workflow variable

Get the time only part of a date time field to a workflow variable

Get the datetime value for the day of the week (Mon, Tues, Wed, Thur, Fri, Sat, Sun) after/before a datetime value and store into a workflow variable

I faced date format issue in fourth requirement, as I found I am not getting user time while getting input from workflow input variable.

Here is the code for above requirements

namespace DateManipulationWorkFlow

{    [CrmWorkflowActivity(“WorkflowInputForDateManipulation”,”DateManipulation”)]

public class DatePartManipulation:SequenceActivity

{        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

        {     try

            {

               //fetch and store shortdate

                this._DateOnly = DateTime.Parse(this.DateInput.Value).ToShortDateString();

                //fetch and store shortime

                this._TimeOnly = DateTime.Parse(this.DateInput.Value).ToShortTimeString();

                //store weekday

                this._WeekDay = DateTime.Parse(this.DateInput.Value).DayOfWeek.ToString();

                //Get day index

                int _DayOfWeek = this.NextDayIndex.Value;

                DateTime _Test = GetNextDate(DateTime.Parse(this.DateInput.Value), _DayOfWeek);

                CrmDateTime _CrmTest = CrmTypes.CreateCrmDateTimeFromUniversal(_Test.ToUniversalTime());

                this._NextWeekDay = _CrmTest;          

    }

            catch (Exception EX)

            {

                string MSG = EX.Message;

            }

            return base.Execute(executionContext);

        }

        //Date input value

        public static DependencyProperty DateInputProperty = DependencyProperty.Register(“DateInput”, typeof(CrmDateTime), typeof(DatePartManipulation));

        [CrmInput(“DateInput”)]

        public CrmDateTime DateInput

        {   get { return (CrmDateTime)GetValue(DateInputProperty); }

            set { SetValue(DateInputProperty, value); }

        }

        //Day Index

        public static DependencyProperty NextDayIndexProperty = DependencyProperty.Register(“NextDayIndex”, typeof(CrmNumber), typeof(DatePartManipulation));

        [CrmInput(“NextDayIndex”)]

        public CrmNumber NextDayIndex

        {

            get { return (CrmNumber)GetValue(NextDayIndexProperty); }

            set

            {

                SetValue(NextDayIndexProperty, value); }

        }

        //Date only output

        public static DependencyProperty _DateOnlyProperty = DependencyProperty.Register(“_DateOnly”, typeof(string), typeof(DatePartManipulation));

        [CrmOutput(“_DateOnly”)]

        public string _DateOnly

        {

            get { return (string)GetValue(_DateOnlyProperty); }

            set { SetValue(_DateOnlyProperty, value); }

        }

        //Time only output

        public static DependencyProperty _TimeOnlyProperty = DependencyProperty.Register(“_TimeOnly”, typeof(string), typeof(DatePartManipulation));

        [CrmOutput(“_TimeOnly”)]

        public string _TimeOnly

        {

            get { return (string)GetValue(_TimeOnlyProperty); }

            set { SetValue(_TimeOnlyProperty, value); }

        }

        //Name of Weekday name

        public static DependencyProperty _WeekDayProperty = DependencyProperty.Register(“_WeekDay”, typeof(string), typeof(DatePartManipulation));

        [CrmOutput(“_WeekDay”)]

        public string _WeekDay

        {

            get { return (string)GetValue(_WeekDayProperty); }

            set { SetValue(_WeekDayProperty, value); }

        }

        //next weekday

        public static DependencyProperty _NextWeekDayProperty = DependencyProperty.Register(“_NextWeekDay”, typeof(CrmDateTime), typeof(DatePartManipulation));

        [CrmOutput(“_NextWeekDay”)]

        public CrmDateTime _NextWeekDay

        {

            get { return (CrmDateTime)GetValue(_NextWeekDayProperty); }

            set { SetValue(_NextWeekDayProperty, value); }

        }

        private int GetWeekDayIndex(string DayofWeek)

        {

            int _intDayIndex = 0;

            switch (DayofWeek.ToUpper())

            {

                case “MONDAY”:

                    _intDayIndex = 1;

                    break;

                case “TUESDAY”:

                    _intDayIndex = 2;

                    break;

                case “WEDNESDAY”:

                    _intDayIndex = 3;

                    break;

                case “THURSDAY”:

                    _intDayIndex = 4;

                    break;

                case “FRIDAY”:

                    _intDayIndex = 5;

                    break;

                case “SATURDAY”:

                    _intDayIndex = 6;

                    break;

                case “SUNDAY”:

                    _intDayIndex = 7;

                    break;

                default:

                    break;

            }      return _intDayIndex;        }

//function to find next weekday depending on day index

        private DateTime GetNextDate(DateTime _StartDate, int DayOfWeek)

        {

            while (GetWeekDayIndex(_StartDate.DayOfWeek.ToString().ToUpper()) != DayOfWeek)

                _StartDate = _StartDate.AddDays(1);    

            return _StartDate;

        }       }}

Advertisements

1 Comment

Filed under MS CRM 4.0, Workflow & Plugin Development

One response to “Custom Workflow To get Datetime for the specified day of the week

  1. Its not my first time to pay a visit this web site, i
    am browsing this web site dailly and take fastidious data from
    here daily.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s