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.
- Find out name of the day when lead is created and set it for Created Day.
- Assign all the leads created during weekend to Alan.
So let’s follow step by step to implement our requirement.
- Modify lead entity and add a new field let’s say “Created Day” of type Text and publish your changes.
- Once we have customized lead entity let’s create custom workflow assembly to find name of the day when lead is created.
- Start Visual Studio and select New Project->Workflow->Activity Library
- Delete “Activity1.xaml” file.
- Right Click on project and select Add New->add a class and name it “LeadAssignment.cs”
- Right Click on project -> select properties and make sure Target Framework is “.Net Framework 4” under Application tab.
- Sign your assembly.
- 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); }
In next post I will show how we can assign lead to Alan.
Pingback: Sending Email with entity attachment from Notes | Mahender Pal
Thanks for sharing this! I love StepByStep notes!
Can anyone tell me what is meant in step 11? I am new to c# so struggling to understand what is required.
Hello Carrie Davis,
Step 11 is about declaring an output variable that will make our output available under Form Assistant, check step number 19 under https://mahenderpal.wordpress.com/2012/08/29/step-by-step-creating-custom-workflow-in-microsoft-crm-2011-part2/
You can get more details about input/out parameters in https://msdn.microsoft.com/en-us/library/gg327984.aspx#InputAttribute
Thanks
Pingback: Sending History Notes using Custom Workflow | HIMBAP