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 always tricky to deal with, when it comes to handle them through program.
In the below code we will Add a break on 6th April 2015 from 12:30 to 1:00 PM:
//First read the UserId for whom you need to add break Guid userid = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId; //Next we need to get the Calendar associated with the user. Every user has a calendar associated with it.
Entity userEntity = service.Retrieve("systemuser", userid, new ColumnSet(new String[] { "calendarid" }));
//Get the calendar for the user
Entity userCalendarEntity = service.Retrieve("calendar", userEntity.GetAttributeValue<EntityReference>("calendarid").Id, new ColumnSet(true));
//The daily schedule of the user is stored in the form of calendar rules.
//Here we are retrieving all the calendar rules related to the particular User Calendar found earlier. EntityCollection calendarRules = (EntityCollection)userCalendarEntity.Attributes["calendarrules"];
What is Inner Calendar?
Inner Calendars are created so that they can be used by other calendars to build different time slots or rules in the System.
// Create a new inner calendar Entity newInnerCalendar = new Entity("calendar"); newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
//create newInnerCalendar Guid innerCalendarId = service.Create(newInnerCalendar);
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.
// Create a new calendar rule and assign the inner calendar id to it Entity calendarRule = new Entity("calendarrule"); calendarRule.Attributes["duration"] = 1440; // 24hrs in minutes
//It specifies the extent of the Calendar rule,generally an Integer value. calendarRule.Attributes["extentcode"] = 1;
//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.
INTERVAL=1; – 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…
COUNT=1; 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.
calendarRule.Attributes["pattern"] = "FREQ=DAILY;INTERVAL=1;COUNT=1"; //Rank is an Integer value which specifies the Rank value of the Calendar rule calendarRule.Attributes["rank"] = 0;
// Timezone code to be set which the calendar rule will follow calendarRule.Attributes["timezonecode"] = 035; //Specifying the InnerCalendar Id calendarRule.Attributes["innercalendarid"] = new EntityReference("calendar", innerCalendarId);
//Start time for the created Calendar rule calendarRule.Attributes["starttime"] = new DateTime(2015, 04, 06, 0, 0, 0, DateTimeKind.Utc);
Now we will add this rule to the earlier retrieved calendar rules
calendarRules.Entities.Add(calendarRule); // assign all the calendar rule back to the user calendar userCalendarEntity.Attributes["calendarrules"] = calendarRules; //update the user calendar entity that has the new rule service.Update(userCalendarEntity); //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.
Calendar rule for Working Day :
Entity workingHourcalendarRule = new Entity("calendarrule"); //Duration of Working hours in minutes (9 hours i.e. 540 minutes) workingHourcalendarRule.Attributes["duration"] = 540; //total work hours duration //Effort available for a resource (User) during the time described by the calendar rule i.e. Capacity part in the Calendar rule workingHourcalendarRule.Attributes["effort"] = 1.0; // It is a Flag used in vary-by-day calendar rules. workingHourcalendarRule.Attributes["issimple"] = true; // offset 480 i.e. 8 hours from start time (12:00) i.e. Working hour start from 8 am
Now, what actually this “offset” mean?
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:
“calendarRule.Attributes["starttime"] = new DateTime(2015, 04, 06, 0, 0, 0, DateTimeKind.Utc);” workingHourcalendarRule.Attributes["offset"] = 480; //to indicate start time is 8 AM //Rank of the Calendar Rule workingHourcalendarRule.Attributes["rank"] = 0; //Sub Type of the Calendar rule.For setting Work hours it is 1. workingHourcalendarRule.Attributes["subcode"] = 1; //Type of calendar rule such as working hours, break, holiday, or time off. 0 for working hours workingHourcalendarRule.Attributes["timecode"] = 0; // Local time zone for the calendar rule. workingHourcalendarRule.Attributes["timezonecode"] = -1; //Specifying the InnerCalendar Id workingHourcalendarRule.Attributes["calendarid"] = new EntityReference("calendar", innerCalendarId);
Calendar rule for Setting Break in the Day from 12:30pm – 1pm
Entity calendarRulebreak = new Entity("calendarrule"); //Duration of Break: 30 minutes calendarRulebreak.Attributes["duration"] = 30; calendarRulebreak.Attributes["effort"] = 1.0; calendarRulebreak.Attributes["issimple"] = true; //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) In our case it will be 12 am + 12.5 hours = 12:30 pm //Duration of Break: 30 minutes calendarRulebreak.Attributes["offset"] = 750; calendarRulebreak.Attributes["rank"] = 0; //SubCode=4 for Specifying Break calendarRulebreak.Attributes["subcode"] = 4; //TimeCode=2 for Specifying Break rule calendarRulebreak.Attributes["timecode"] = 2; // Local time zone for the calendar rule calendarRulebreak.Attributes["timezonecode"] = -1; calendarRulebreak.Attributes["calendarid"] = new EntityReference("calendar", innerCalendarId); //Add working hour rule to InnerCalendar Rules innerCalendarRules.Entities.Add(workingHourcalendarRule); //Add Break Hour rule to InnerCalendar Rules innerCalendarRules.Entities.Add(calendarRulebreak); newInnerCalendar.Attributes["calendarrules"] = innerCalendarRules; newInnerCalendar.Attributes["calendarid"] = innerCalendarId; service.Update(newInnerCalendar);
In the new Inner Calendar which we created we will add the above two calendar rules and update the Inner calendar.
After successfully running the code you may see a record is created for the specified date with a Break added as seen below:
Conclusion:
The steps to get the calendar is
- Read the user calendar
- Create the inner calendar
- Create the calendar rule for the day
- Create the calendar rule for the work duration
- Create the calendar rule for the break duration.
There's much more, for more Dynamics CRM Tips and Tricks head on to Inogic Blog. If you have any questions on Dynamics CRM or need any help in implementation, customization, upgradation of Dynamics CRM or are looking to get more out of your CRM, feel free to reach us at crm@inogic.com today!
The post Creating Calendar rules with Breaks in Microsoft Dynamics CRM 2015 appeared first on Inogic Blog.
*This post is locked for comments