Pages

Monday, 29 April 2013

How to Create Custom Workflow in CRM 2011

Scenario: Company A used to get many leads on weekend, but none of their existing sales executive wants to work on weekend, so they have recently recruited one part time sales person Suresh who will be working on weekends. So we have a requirement to assign all the leads created during weekends to Suresh and we also need to display Created Day in lead records.

We have to follow below steps to run custom workflow.
1.       Find out name of the day when lead is created and set it for Created Day.
2.       Assign all the leads created during weekend to Alan.
So let’s follow step by step to implement our requirement.
1.  Modify lead entity and add a new field let’s say “Created Day” of type Text and publish your changes.
2.  Once we have customized lead entity let’s create custom workflow assembly to find name of the day when lead is created.

Create project for Custom Workflow:

1.  Start Visual Studio and select New Project->Workflow->Activity Library
2.  Delete “Activity1.xaml” file.
3.  Right Click on project and select Add New->add a class and name it “LeadCreatedDay.cs”
4.  Right Click on project -> select properties and make sure Target Framework is “.Net Framework 4” under Application tab.
5.  Sign your assembly.
6.  Right Click on project and select Add Reference to add required assemblies to our project.
We need to add below Microsoft CRM 2011 SDK assemblies and .net assemblies
Microsoft.xrm.sdk
Microsoft.xrm.sdk.workflow
Microsoft.crm.sdk.proxy
System.Runtime.Serialization
C# Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;

namespace DayOfWeek
{
    public class LeadCreatedDay : CodeActivity
    {
        [Output("DayofWeek")]
        public OutArgument<String> DayofWeek { get; set; }

        protected override void Execute(CodeActivityContext Execution)
        {
            string Day = string.Empty;

            DateTime _Date = DateTime.MinValue;

            //get context

            IWorkflowContext context = Execution.GetExtension<IWorkflowContext>();

            //create iorganization service object

            IOrganizationServiceFactory serviceFactory = Execution.GetExtension<IOrganizationServiceFactory>();

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

            //get created date of the lead

            Entity _lead = (Entity)service.Retrieve("lead", context.PrimaryEntityId, new ColumnSet(new string[] { "createdon" }));

            if (_lead.Contains("createdon"))
            {
                //get day of the week based on created on date
                Day = ((DateTime)_lead.Attributes["createdon"]).DayOfWeek.ToString();
            }
            //set value to output variable
            DayofWeek.Set(Execution, Day);
            if (Day == "Sunday")
            {
                Guid _UserID = GetUserID(service);

                Assignlead(service, context.PrimaryEntityId, _UserID);

            }
        }

        private Guid GetUserID(IOrganizationService service) //function to get userid
        {
            Guid _UserID = Guid.Empty;

            ConditionExpression condition1 = new ConditionExpression();

            condition1.AttributeName = "firstname";

            condition1.Operator = ConditionOperator.Equal;

            condition1.Values.Add("Suresh");

            ConditionExpression condition2 = new ConditionExpression();

            condition2.AttributeName = "lastname";

            condition2.Operator = ConditionOperator.Equal;

            condition2.Values.Add("Kumar");

            FilterExpression filter1 = new FilterExpression();

            filter1.Conditions.AddRange(condition1, condition2);

            QueryExpression query = new QueryExpression("systemuser");

            query.Criteria.AddFilter(filter1);

            EntityCollection EntityCol = service.RetrieveMultiple(query);

            if (EntityCol.Entities.Count > 0)
            {
                Entity _User = (Entity)EntityCol.Entities[0];

                _UserID = _User.Id;

            }

            return _UserID;

        }
        private void Assignlead(IOrganizationService service, Guid LeadID, Guid UserID)
        {

            AssignRequest _Assign = new AssignRequest()
            {

                Assignee = new EntityReference("systemuser", UserID),

                Target = new EntityReference("lead", LeadID)

            };

            service.Execute(_Assign);

        }
    }
}
Image for add steps for workflow when lead is created:

Follow the below image and add steps in your workflow when lead is created.

Don't remember to click on the Activate button.


No comments:

Post a Comment