Sudeep
Sudeep

Reputation: 343

Time off entry in Online CRM 2015 using C# (plugin or otherwise)

I wish to do the following (I am using .net 4.5, online crm-2015)

  1. A particular Equipment and Facility resource may not be available for scheduling on a given day(s)
  2. It is to be done through code via C# in a plugin or a simple web application
  3. User will choose the Equipment/Facility, the day(s) for the time off.
  4. On the chosen day that resource should not be available for any Service activity(via service calendar in CRM UI).

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:

  1. Nothing shows up on the facility calendar(work hours).

  2. 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.

  3. I am unable to change the working hours after creating this record for the same facility.
  4. Under Setting->Service Mgmt->Holiday Schedule. I can see the new record, without any name, but when I try to open the same, it gives a CRM error. You cannot delete this records too
  5. Once I delete the Calendar rules(through code), then I can go back to Service Mgmt->Holiday Schedule and delete the records, once that is done, I can change the working hour of the facility too.

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

Answers (1)

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

Related Questions