{"id":1967,"date":"2015-11-18T17:13:54","date_gmt":"2015-11-18T11:43:54","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1967"},"modified":"2015-11-18T17:13:54","modified_gmt":"2015-11-18T11:43:54","slug":"creating-calendar-rules-with-breaks-in-microsoft-dynamics-crm-2015","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/11\/creating-calendar-rules-with-breaks-in-microsoft-dynamics-crm-2015\/","title":{"rendered":"Creating Calendar rules with Breaks in Microsoft Dynamics CRM 2015"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>Microsoft Dynamics CRM has comprehensive Calendar management capabilities to manage work schedules.<\/p>\n<p>You are allowed to not only specify the work hours but also the break times when the resource would be unavailable.<\/p>\n<p>Here we are demonstrating how to add breaks to daily work schedule programmatically.<\/p>\n<p>Calendar and Calendar rules entities in CRM are always tricky to deal with, when it comes to handle them through program.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Sample Work Schedule<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/11\/break.png\"><img decoding=\"async\" class=\"aligncenter wp-image-1968 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/11\/break.png\" alt=\"Calendar rules\" width=\"630\" height=\"388\" \/><\/a><\/strong><\/span><\/p>\n<p>In the below code we will Add a break on 6<sup>th<\/sup> April 2015 from 12:30 to 1:00 PM:<\/p>\n<pre class=\"lang:default decode:true\">\/\/First read the UserId for whom you need to add break\nGuid userid = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;\n\/\/Next we need to get the Calendar associated with the user. Every user has a calendar associated with it.\nEntity userEntity = service.Retrieve(\"systemuser\", userid, new ColumnSet(new String[] { \"calendarid\" }));\n\/\/Get the calendar for the user\nEntity userCalendarEntity = service.Retrieve(\"calendar\", userEntity.GetAttributeValue&lt;EntityReference&gt;(\"calendarid\").Id, new ColumnSet(true));\n\/\/The daily schedule of the user is stored in the form of calendar rules.\n\/\/Here we are retrieving all the calendar rules related to the particular User Calendar found earlier.\nEntityCollection calendarRules = (EntityCollection)userCalendarEntity.Attributes[\"calendarrules\"];\n<\/pre>\n<p><strong>What is Inner Calendar?<\/strong><\/p>\n<p>Inner Calendars are created so that they can be used by other calendars to build different time slots or rules in the System.<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Create a new inner calendar\nEntity newInnerCalendar = new Entity(\"calendar\");\n\nnewInnerCalendar.Attributes[\"businessunitid\"] = new EntityReference(\"businessunit\", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity[\"businessunitid\"])).Id);\n\n\/\/create newInnerCalendar\nGuid innerCalendarId = service.Create(newInnerCalendar);\n<\/pre>\n<p>First we are creating a calendar rule specifying the duration to be 1440 minutes i.e. 24 hours as we want to create this rule for one day only.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Create a new calendar rule and assign the inner calendar id to it\nEntity calendarRule = new Entity(\"calendarrule\");\n\ncalendarRule.Attributes[\"duration\"] = 1440; \/\/ 24hrs in minutes\n\n\/\/It specifies the extent of the Calendar rule,generally an Integer value.\ncalendarRule.Attributes[\"extentcode\"] = 1;\n<\/pre>\n<p>\/\/Pattern of the rule recurrence. As we have given FREQ=DAILY it will create a calendar rule on daily basis. We can even create on Weekly basis by specifying FREQ=WEEKLY.<\/p>\n<p><span style=\"color: #800000;\">INTERVAL=1;<\/span> &#8211; This means how many days interval between the next same schedule. For e.g if the date was 6th April and interval was 2, it would create a schedule for 8th april, 10th april and so on\u2026<\/p>\n<p><span style=\"color: #800000;\">COUNT=1;<\/span> This means how many recurring records should be created, if in the above example the count was given as 2, it would create schedule for 6th and 8th and then stop. If the count was 3, it would go on until 10th and then stop<\/p>\n<pre class=\"lang:default decode:true\">calendarRule.Attributes[\"pattern\"] = \"FREQ=DAILY;INTERVAL=1;COUNT=1\";\n\/\/Rank is an Integer value which specifies the Rank value of the Calendar rule\ncalendarRule.Attributes[\"rank\"] = 0;\n\n\/\/ Timezone code to be set which the calendar rule will follow\ncalendarRule.Attributes[\"timezonecode\"] = 035;\n\n\/\/Specifying the InnerCalendar Id\ncalendarRule.Attributes[\"innercalendarid\"] = new EntityReference(\"calendar\", innerCalendarId);\n\n\/\/Start time for the created Calendar rule\ncalendarRule.Attributes[\"starttime\"] = new DateTime(2015, 04, 06, 0, 0, 0, DateTimeKind.Utc);<\/pre>\n<p>Now we will add this rule to the earlier retrieved calendar rules<\/p>\n<pre class=\"lang:default decode:true \">calendarRules.Entities.Add(calendarRule);\n\n\/\/ assign all the calendar rule back to the user calendar\nuserCalendarEntity.Attributes[\"calendarrules\"] = calendarRules;\n\/\/update the user calendar entity that has the new rule\nservice.Update(userCalendarEntity);\n\n\/\/Above we created a Calendar rule, now below we are creating two more Calendar rules one specifying the Working hour in the day (i.e. on 6th April 2015) and other for specifying the Break time (30 minutes) in a day.<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Calendar rule for Working Day :<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">Entity workingHourcalendarRule = new Entity(\"calendarrule\");\n\n\/\/Duration of Working hours in minutes (9 hours i.e. 540 minutes)\nworkingHourcalendarRule.Attributes[\"duration\"] = 540; \/\/total work hours duration\n\n\/\/Effort available for a resource (User) during the time described by the calendar rule i.e. Capacity part in the Calendar rule\nworkingHourcalendarRule.Attributes[\"effort\"] = 1.0;\n\n\/\/ It is a Flag used in vary-by-day calendar rules.\nworkingHourcalendarRule.Attributes[\"issimple\"] = true;\n\n\/\/ offset 480 i.e. 8 hours from start time (12:00) i.e. Working hour start from 8 am<\/pre>\n<p>Now, what actually this \u201coffset\u201d mean?<\/p>\n<p>In the earlier Calendar rule which we updated in CRM we had set the start time by default to 12 am by setting the below attribute:<\/p>\n<pre class=\"lang:default decode:true \">\u201ccalendarRule.Attributes[\"starttime\"] = new DateTime(2015, 04, 06, 0, 0, 0, DateTimeKind.Utc);\u201d\n\nworkingHourcalendarRule.Attributes[\"offset\"] = 480; \/\/to indicate start time is 8 AM\n\n\/\/Rank of the Calendar Rule\nworkingHourcalendarRule.Attributes[\"rank\"] = 0;\n\n\/\/Sub Type of the Calendar rule.For setting Work hours it is 1.\nworkingHourcalendarRule.Attributes[\"subcode\"] = 1;\n\n\/\/Type of calendar rule such as working hours, break, holiday, or time off. 0 for working hours\nworkingHourcalendarRule.Attributes[\"timecode\"] = 0;\n\n\/\/ Local time zone for the calendar rule.\nworkingHourcalendarRule.Attributes[\"timezonecode\"] = -1;\n\n\/\/Specifying the InnerCalendar Id\n\nworkingHourcalendarRule.Attributes[\"calendarid\"] = new EntityReference(\"calendar\", innerCalendarId);<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Calendar rule for Setting Break in the Day from 12:30pm \u2013 1pm<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">Entity calendarRulebreak = new Entity(\"calendarrule\");\n\n\/\/Duration of Break: 30 minutes\ncalendarRulebreak.Attributes[\"duration\"] = 30;\n\ncalendarRulebreak.Attributes[\"effort\"] = 1.0;\n\ncalendarRulebreak.Attributes[\"issimple\"] = true;\n\n\/\/We setting the offset to 750minutes (i.e. 12.5 hours) .As explained for above part here we are specifying when this rule will start. (I.e. when the break starts)\nIn our case it will be 12 am + 12.5 hours = 12:30 pm\n\/\/Duration of Break: 30 minutes\ncalendarRulebreak.Attributes[\"offset\"] = 750;\n\ncalendarRulebreak.Attributes[\"rank\"] = 0;\n\n\/\/SubCode=4 for Specifying Break\ncalendarRulebreak.Attributes[\"subcode\"] = 4;\n\n\/\/TimeCode=2 for Specifying Break rule\ncalendarRulebreak.Attributes[\"timecode\"] = 2;\n\n\/\/ Local time zone for the calendar rule\ncalendarRulebreak.Attributes[\"timezonecode\"] = -1;\n\ncalendarRulebreak.Attributes[\"calendarid\"] = new EntityReference(\"calendar\", innerCalendarId);\n\n\/\/Add working hour rule to InnerCalendar Rules\ninnerCalendarRules.Entities.Add(workingHourcalendarRule);\n\n\/\/Add Break Hour rule to InnerCalendar Rules\ninnerCalendarRules.Entities.Add(calendarRulebreak);\n\nnewInnerCalendar.Attributes[\"calendarrules\"] = innerCalendarRules;\nnewInnerCalendar.Attributes[\"calendarid\"] = innerCalendarId;\n\nservice.Update(newInnerCalendar);<\/pre>\n<p>In the new Inner Calendar which we created we will add the above two calendar rules and update the Inner calendar.<\/p>\n<p>After successfully running the code you may see a record is created for the specified date with a Break added as seen below:<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/11\/break.png\"><img decoding=\"async\" class=\"aligncenter wp-image-1968 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/11\/break.png\" alt=\"Creating Calendar rules with Breaks\" width=\"630\" height=\"388\" \/><\/a><\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>The steps to get the calendar is<\/p>\n<ol>\n<li>Read the user calendar<\/li>\n<li>Create the inner calendar<\/li>\n<li>Create the calendar rule for the day<\/li>\n<li>Create the calendar rule for the work duration<\/li>\n<li>Create the calendar rule for the break duration.<\/li>\n<\/ol>\n<p>Get more from your Dynamics CRM..Start monitoring <a href=\"http:\/\/inogic.com\/Product\/100\/Add-Ons\/User-Adoption-Tracker\" target=\"_blank\" rel=\"noopener noreferrer\">User Adoption <\/a>today!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Microsoft Dynamics CRM has comprehensive Calendar management capabilities to manage work schedules. You are allowed to not only specify the work hours but also the break times when the resource would be unavailable. Here we are demonstrating how to add breaks to daily work schedule programmatically. Calendar and Calendar rules entities in CRM are\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/11\/creating-calendar-rules-with-breaks-in-microsoft-dynamics-crm-2015\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,15,19,22,24],"tags":[240,597],"class_list":["post-1967","post","type-post","status-publish","format-standard","hentry","category-customer-service","category-development","category-dynamics-crm","category-dynamics-crm-2015","category-dynamics-crm-2016","tag-calender-rules","tag-dynamics-crm-2015"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=1967"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1967\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}