Reputation: 343
I wish to do the following (I am using .net 4.5, online crm-2015)
I have written this code for the time off. Code is copied from the internet.
// svc is the IOrgService already instantiated
//Guid facilityID =this is the id for the facility
//Get the calendar id of the facility
Entity facilityEntity = svc.Retrieve(CrmEarlyBound.Equipment.EntityLogicalName, facilityID, new ColumnSet(new String[] { "calendarid" }));
// Retrieve the calendar of the facility(whole entity)
Entity facilityCalendarEntity = svc.Retrieve("calendar", ((Microsoft.Xrm.Sdk.EntityReference)(facilityEntity.Attributes["calendarid"])).Id
, new ColumnSet(true));
// Retrieve the calendar rules defined in the calendar
EntityCollection calendarRules = (EntityCollection)facilityCalendarEntity.Attributes["calendarrules"];
//Create a new inner calendar
CrmEarlyBound.Calendar newInnerCalendar = new CrmEarlyBound.Calendar();
newInnerCalendar.Type = new OptionSetValue(-1);
newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
newInnerCalendar.Description = "my description";
newInnerCalendar.Name = "cal1";
Guid innerCalendarId = svc.Create(newInnerCalendar);
CrmEarlyBound.Calendar newInnerCalendar = new CrmEarlyBound.Calendar();
newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(facilityCalendarEntity["businessunitid"])).Id);
newInnerCalendar.Description = "my description";
newInnerCalendar.Name = "cal1";
Guid innerCalendarId = svc.Create(newInnerCalendar);
// Create a new calendar rule and assign the inner calendar id to it
Entity calendarRule = new Entity("calendarrule");
calendarRule.Attributes["description"] = "Time Off Rule";
calendarRule.Attributes["duration"] = 1440;
calendarRule.Attributes["extentcode"] = 2;
calendarRule.Attributes["pattern"] = "FREQ=DAILY;INTERVAL=1;COUNT=1";
calendarRule.Attributes["rank"] = 0;
calendarRule.Attributes["timezonecode"] = 85;
calendarRule.Attributes["innercalendarid"] = new EntityReference("calendar", innerCalendarId);
// starting at 12:00 on 7 May
calendarRule.Attributes["starttime"] = new DateTime(2016, 5, 10, 0, 0, 0, DateTimeKind.Utc);
calendarRules.Entities.Add(calendarRule);
//assign all the calendar rule back to the user calendar
facilityCalendarEntity.Attributes["calendarrules"] = calendarRules;
//update the user calendar entity that has the new rule
svc.Update(facilityCalendarEntity);
Now the problems:
Nothing shows up on the facility calendar(work hours).
If you do the same thing through CRM UI, a red mark is shown on the days off, in my case no indication is there.
Could you tell me what am I doing wrong and how will you write the code for scheduling time off for facility and equipment.
Upvotes: 1
Views: 941
Reputation: 22836
Couple of issues.
First, the below piece of code has to be removed. This has same variable used for creation for unnecessary record.
CrmEarlyBound.Calendar newInnerCalendar = new CrmEarlyBound.Calendar();
newInnerCalendar.Type = new OptionSetValue(-1);
newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
newInnerCalendar.Description = "my description";
newInnerCalendar.Name = "cal1";
Guid innerCalendarId = svc.Create(newInnerCalendar);
Second, you have to create root/leaf calendar rule.
Entity calendarRule1 = new Entity("calendarrule");
// duration of 8 hours
calendarRule1.Attributes["duration"] = 480;
calendarRule1.Attributes["effort"] = 2.0;
calendarRule1.Attributes["issimple"] = true;
calendarRule1.Attributes["offset"] = 0;
calendarRule1.Attributes["rank"] = 0;
// subcode 6= vacation
calendarRule1.Attributes["subcode"] = 6;
// time code 2 = unavailable
calendarRule1.Attributes["timecode"] = 2;
calendarRule1.Attributes["timezonecode"] = -1;
EntityCollection innerCalendarRules = new EntityCollection();
innerCalendarRules.EntityName = "calendarrule";
innerCalendarRules.Entities.Add(calendarRule1);
newInnerCalendar.Attributes["calendarrules"] = innerCalendarRules;
newInnerCalendar.Attributes["calendarid"] = innerCalendarId;
organizationProxy.Update(newInnerCalendar);
Upvotes: 0