使用C#(插件或其他)在Online CRM 2015中暂停

时间:2015-10-16 07:25:30

标签: c# dynamics-crm dynamics-crm-online dynamics-crm-2015

我希望执行以下操作(我使用.net 4.5,在线crm-2015)

  1. 特定设备和设施资源可能无法在指定日期安排
  2. 通过C#在插件或简单的Web应用程序中通过代码来完成
  3. 用户将选择设备/设施,休息日。
  4. 在选定日期,资源不应用于任何服务活动(通过CRM用户界面中的服务日历)。
  5. 我已经把这段代码写下来了。代码为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);
    

    现在出现问题:

    1. 设施日历上没有任何内容(工作时间)。

    2. 如果您通过CRM用户界面执行相同操作,则会在休息日显示红色标记,在我的情况下没有任何指示。

    3. 在为同一设施创建此记录后,我无法更改工作时间。
    4. 在设置 - >服务管理 - >假期计划下。我可以看到新记录,没有任何名称,但当我尝试打开相同时,它会给出一个CRM错误。您也无法删除此记录
    5. 一旦我删除了日历规则(通过代码),我就可以返回服务管理 - >假期计划并删除记录,一旦完成,我也可以更改设施的工作时间。
    6. 你能告诉我我做错了什么,你将如何编写用于安排设施和设备休假的代码。

1 个答案:

答案 0 :(得分:0)

一些问题。

首先,必须删除下面的代码。这个变量用于创建不必要的记录。

        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);

其次,您必须创建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);