web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Step By Step Creating Custom Workflow in Microsoft CRM

(1) ShareShare
ReportReport
Posted on by

If you are a fresher in Microsoft CRM development and want to learn how to write custom workflow for Microsoft CRM 2011/2013/2015, then this post is for you. Let’s consider one scenario Company Xrm 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 Alan who will be working on weekends. So we have a requirement to assign all the leads created during weekends to Alan and we also need to display Created Day in lead records. So keeping this scenario in mind, we have two requirement.

  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.
  3. Start Visual Studio and select New Project->Workflow->Activity Library
  4. Delete “Activity1.xaml” file.
  5. Right Click on project and select Add New->add a class and name it “LeadAssignment.cs”
  6. Right Click on project -> select properties and make sure Target Framework is “.Net Framework 4” under Application tab.
  7. Sign your assembly.
  8. 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

9. Double click on “LeadAssignment.cs” and add below using directive to your class.

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

10. Modify your class definition like below to inherit

public class LeadAssignment:CodeActivity

11. Declare output variable of string type and declare it’s property like below

[Output("DayofWeek")]

public OutArgument<String>DayofWeek { get; set; }

//output variables are used to provide response to user when user will select this attribute from form assistant

12. Now add below execute method to our class

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);

}

we have completed code to get lead created day, so let’s now write function to get user id for Alan and assign all lead to Alan if lead created day is Sunday.

13. Create function to get userid like below

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("Alan");

ConditionExpression condition2 = new ConditionExpression();

condition2.AttributeName = "lastname";

condition2.Operator = ConditionOperator.Equal;

condition2.Values.Add("Smith");

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.FirstOrDefault();

_UserID = _User.Id;

}

return _UserID;

}

14. Write a function to assign passed lead to Alan, like below

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);

}
finally we need to modify our Execute method and add below lines
if (Day == "Sunday")

{
Guid _UserID = GetUserID(service);

Assignlead(service, context.PrimaryEntityId, _UserID);

}
I have the same question (0)
  • necsa Profile Picture
    3,455 on at
    RE: Step By Step Creating Custom Workflow in Microsoft CRM

    Why didn't you explain thata rticle you publish it 8 years ago.

  • necsa Profile Picture
    3,455 on at
    RE: Step By Step Creating Custom Workflow in Microsoft CRM

    You are a dishonest person. Don't share some article which is not belong to you. If you share something from other person just give the source from whom is it.

    This article belong to Mahender and he publish it in August 2012. You just stolen and share after 8 years as your article.

    Here is the link from original article and it is word to word exactly same.

    https://himbap.com/blog/?p=791

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: Step By Step Creating Custom Workflow in Microsoft CRM

    Hello,

    Forum is not for posting any blogs instead you can discuss any issue if you are facing. I am not sure what is the point of Sharing this post , as we already have blog posts from community members under Resources.

  • LuHao Profile Picture
    40,888 on at
    RE: Step By Step Creating Custom Workflow in Microsoft CRM

    Thanks for your sharing.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 150

#2
DAnny3211 Profile Picture

DAnny3211 73

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 66 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans