{"id":780,"date":"2012-08-26T05:41:33","date_gmt":"2012-08-26T05:41:33","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=780"},"modified":"2018-07-08T10:40:38","modified_gmt":"2018-07-08T10:40:38","slug":"step-by-step-creating-custom-workflow-in-microsoft-crm-2011","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=780","title":{"rendered":"Step By Step Creating Custom Workflow in Microsoft CRM Part 1"},"content":{"rendered":"<p>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\u2019s 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.<\/p>\n<ol>\n<li>Find out name of the day when lead is created and set it for Created Day.<\/li>\n<li>Assign all the leads created during weekend to Alan.<\/li>\n<\/ol>\n<p>So let\u2019s follow step by step to implement our requirement.<\/p>\n<ol>\n<li>Modify lead entity and add a new field let\u2019s say \u201cCreated Day\u201d of type Text and publish your changes.<\/li>\n<li>Once we have customized lead entity let\u2019s create custom workflow assembly to find name of the day when lead is created.<\/li>\n<li>Start Visual Studio and select New Project-&gt;Workflow-&gt;Activity Library<\/li>\n<li>Delete \u201cActivity1.xaml\u201d file.<\/li>\n<li>Right Click on project and select Add New-&gt;add a class and name it \u201cLeadAssignment.cs\u201d<\/li>\n<li>Right Click on project -&gt; select properties and make sure Target Framework is \u201c.Net Framework 4\u201d under Application tab.<\/li>\n<li>Sign your assembly.<\/li>\n<li>Right Click on project and select Add Reference to add required assemblies to our project.<\/li>\n<\/ol>\n<p>We need to add below Microsoft CRM 2011 SDK assemblies and .net assemblies<\/p>\n<p><strong>Microsoft.xrm.sdk<\/strong><\/p>\n<p><strong>Microsoft.xrm.sdk.workflow<\/strong><\/p>\n<p><strong>Microsoft.crm.sdk.proxy<\/strong><\/p>\n<p><strong>System.Runtime.Serialization<\/strong><\/p>\n<p>9. Double click on \u201cLeadAssignment.cs\u201d and add below using directive to your class.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nusing Microsoft.Xrm.Sdk.Query;\r\nusing System.Activities;\r\nusing Microsoft.Xrm.Sdk.Workflow;\r\nusing Microsoft.Xrm.Sdk;\r\nusing Microsoft.Crm.Sdk.Messages;\r\n<\/pre>\n<p>10. Modify your class definition like below to inherit<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class LeadAssignment:CodeActivity\r\n<\/pre>\n<p>11. Declare output variable of string type and declare it\u2019s property like below<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n[Output(&quot;DayofWeek&quot;)]\r\n\r\npublic OutArgument&lt;String&gt;DayofWeek { get; set; }\r\n\r\n\/\/output variables are used to provide response to user when user will select this attribute from form assistant\r\n<\/pre>\n<p>12. Now add below execute method to our class<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void Execute(CodeActivityContext Execution)\r\n\r\n{\r\n\r\nstring Day = string.Empty;\r\n\r\nDateTime _Date = DateTime.MinValue;\r\n\r\n\/\/get context\r\n\r\nIWorkflowContext context = Execution.GetExtension&lt;IWorkflowContext&gt;();\r\n\r\n\/\/create iorganization service object\r\n\r\nIOrganizationServiceFactory serviceFactory =\r\n\r\nExecution.GetExtension&lt;IOrganizationServiceFactory&gt;();\r\n\r\nIOrganizationService service =\r\n\r\nserviceFactory.CreateOrganizationService(context.InitiatingUserId);\r\n\r\n\/\/get created date of the lead\r\n\r\nEntity _lead = (Entity)service.\r\n\r\nRetrieve(&quot;lead&quot;, context.PrimaryEntityId, new ColumnSet(\r\n\r\nnew string[] { &quot;createdon&quot; }));\r\n\r\nif (_lead.Contains(&quot;createdon&quot;))\r\n\r\n{\r\n\r\n\/\/get day of the week based on created on date\r\n\r\nDay = ((DateTime)_lead.Attributes[&quot;createdon&quot;]).DayOfWeek.ToString();\r\n\r\n}\r\n\r\n\/\/set value to output variable\r\n\r\nDayofWeek.Set(Execution, Day);\r\n\r\n}\r\n<\/pre>\n<p>In next post I will show how we can\u00a0assign lead to Alan.<\/p>\n<h2><a href=\"https:\/\/himbap.com\/blog\/?p=791\">Step By Step Creating Custom Workflow in Microsoft CRM &#8211; Part2<\/a><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s 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&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=780\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,6,8,19],"tags":[240,55,239],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/780"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=780"}],"version-history":[{"count":7,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions"}],"predecessor-version":[{"id":3064,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions\/3064"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}