Custom Workflow (Real Time Workflow) with Input and OutPut Parameters in D365




using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomWorkflowAgeCalByDOB
{
    //DOB Calculate by using custom workflow (Real Time Workflow) with Input and OutPut Parameters
    /// <summary>
    /// Register a Workflow on Contact Entity on Create or Update(DOB filed) Event
    /// Create fields DOB DateTime and Age,Days of WholeNumber type in Contact entity
    /// Select the Custom workflow and fill the DOB
    /// In Update step Setproperties Age and day field
    /// </summary>
    public class Class1 : CodeActivity
    {
        [Input("Date of Birth")]
        public InArgument<DateTime> DOB { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            ITracingService tracingService = (ITracingService)context.GetExtension<ITracingService>();
            IWorkflowContext workflowContext = (IWorkflowContext)context.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)context.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

            DateTime dtDOB = DOB.Get(context);
            int CalculateAges = Convert.ToInt32(DateTime.Now.Subtract(dtDOB).TotalDays)/365;
            int CalculateDays = Convert.ToInt32(DateTime.Now.Subtract(dtDOB).TotalDays);
            Day.Set(context, CalculateDays);
            Age.Set(context, CalculateAges);
        }
        [Output("Age")]
        public OutArgument<Int32> Age { get; set; }
        [Output("Day")]
        public OutArgument<Int32> Day { get; set; }
    }
}


Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#