Wednesday 24 May 2017

CUSTOM WORKFLOW ACTIVITY SAMPLE CODE FOR DYNAMICS 365

Here is a simple scenario

1) Create two fileds
a) DOB,
b) AGE

2) Calculate AGE based on DOB selected..

So we need to write a simple custom workflow as follows:





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


namespace CustomWorkflowActivity
{
    public class CalculateAge : 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 CalculateAge = Convert.ToInt32(DateTime.Now.Subtract(dtDOB).TotalDays)/365;
            Age.Set(context, CalculateAge);
        }
        [Output("Age")]
        public OutArgument<Int32> Age { get; set; }
    }
}


======================================================================

After creating Custom workflow then use the custom workflow activity in the processes section to calculate the Age of the contact..



I hope this helps...

Happy CRMing:-)
↜Ҝ



No comments:

Post a Comment

Note: only a member of this blog may post a comment.