Search code examples
dynamics-crmcrmmicrosoft-dynamicsdynamics-crm-2016

How to get current date in workflows (Dynamics CRM 2016)


I would ask about how to get current date in workflows.?

For example:-

I have field called (Inspection Date) I would like to compare this filed if it equal current date then go into the process.

I will create Wait Condition waiting until current date = Inspection date then will do my process .

How to get current date in workflow.?


Solution

  • To answer the OP's question about how to get the current date in the workflow, the short answer is that you can't. As Henk van Boeijen has described, it is possible to get the current date in some of the individual steps however.

    Where I work we have implemented a very basic custom workflow activity that simply returns the current date and time.

    public class CurrentDateWorkflow : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                // Create the context and tracing service
                IExecutionContext context = executionContext.GetExtension<IExecutionContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                ITracingService tracingService = executionContext.GetExtension<ITracingService>();
                if (tracingService == null)
                    throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
    
                tracingService.Trace("Entered CurrentDateWorkflow.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                    executionContext.ActivityInstanceId,
                    executionContext.WorkflowInstanceId);
    
                var DatePartOnly = InputDatePartOnly.Get(executionContext);
    
                // Set output parameters
                if (DatePartOnly)
                    CurrentDate.Set(executionContext, DateTime.UtcNow.Date);
                else
                    CurrentDate.Set(executionContext, DateTime.UtcNow);
    
                // All done
                tracingService.Trace("CurrentDateWorkflow.Execute() Complete. Activity Instance Id: {0}, Workflow Instance Id: {1}",
                    executionContext.ActivityInstanceId,
                    executionContext.WorkflowInstanceId);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Format("An error occurred in the {0} plug-in.",
                        this.GetType().ToString()),
                        ex);
            }
        }
    
        [Output("Current Date")]
        public OutArgument<DateTime> CurrentDate { get; set; }
    
        [Input("Date Only")]
        [Default("False")]
        public InArgument<bool> InputDatePartOnly { get; set; }
    }