2014-10-20 55 views
0

更新插件,我需要編寫插件在CRM 2013是做兩兩件事:創建和CRM 2013的C#

  1. 如果statecode = 3和外地el_meeting_in_outlook_id是空的我 需要創建一個新的appoitment。
  2. 如果statecode = 3且字段el_meeting_in_outlook_id不爲空 我需要更新現有的appoitment。

這是我寫的:

using Microsoft.Xrm.Sdk; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using tmura_Entity_Plugins; 

namespace tmura_Entity__Plugins 
{ 
    public class postCreateUpdateServiceAppointment : Plugin 
    { 
     public postCreateUpdateServiceAppointment() 
      : base(typeof(postCreateUpdateServiceAppointment)) 
     { 
      base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", null, new Action<LocalPluginContext>(ExecutePostCreate))); 
      base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", null, new Action<LocalPluginContext>(ExecutePostUpdate))); 

     } 

     private void ExecutePostCreate(LocalPluginContext obj) 
     { 
      Logger.WriteMessage("Enter ExecutePostCreate", CrmLogService.MessageLevel.Info, ""); 
      if ((obj.PluginExecutionContext.InputParameters.Contains("Target")) && (obj.PluginExecutionContext.InputParameters["Target"] is Entity)) 
      { 
       Entity serviceAppontment = (Entity)obj.PluginExecutionContext.InputParameters["Target"]; 
       if (serviceAppontment.LogicalName != "serviceappointment") 
        return; 

       Logger.WriteMessage("", CrmLogService.MessageLevel.Info, ""); 

       if ((serviceAppontment.Attributes.Contains("statecode")) || ((int)(serviceAppontment.Attributes["statecode"]) == 3) && (serviceAppontment.Attributes["el_meeting_in_outlook_id"] == null)) 
       { 
        try 
        { 
         Entity appointment = new Entity("appointment"); 
         appointment.Attributes.Add("subject", "Opened automatically"); 
         appointment.Attributes.Add("description", "Just Checking"); 
         appointment.Attributes.Add("el_serviceappointment_id", new EntityReference("serviceappointment", serviceAppontment.Id)); 
         appointment.Attributes.Add("scheduledstart", DateTime.Now.AddDays(7)); 
         appointment.Attributes.Add("scheduledend", DateTime.Now.AddDays(7)); 

         obj.OrganizationService.Create(appointment); 
        }      
        catch (Exception ex) 
        { 
         Logger.WriteException(ex); 
        } 
       } 
       else if (((int)(serviceAppontment.Attributes["statecode"]) == 3) && (serviceAppontment.Attributes["el_meeting_in_outlook_id"] != null)) 
       { 
        //TODO 
       } 
      } 
     } 
    } 
} 

我不知道在一個應該更新預約第二部分寫。我試圖搜索網頁,但沒有成功。

你能幫忙嗎?

+0

我可以問你爲什麼在POST操作中註冊了插件嗎?它會在相同的實體記錄上兩次導致UPDATE。第一次在實際過程中,第二次通過您的插件觸發UPDATE。它在PRE操作上註冊您的插件,在給定的情況下更適合。另一方面,PRE插件只會更新一次。 – Scorpion 2014-10-20 15:01:45

+0

好吧,這是需求任務。我會和我的經理覈對一下。謝謝! – userS 2014-10-21 03:04:17

回答

0

如果您定義了註冊事件,則必須將「serviceappointment」(即實體邏輯名稱)而不是null。

然後替換:(serviceAppontment.Attributes.Contains("statecode")) || ((int)(serviceAppontment.Attributes["statecode"]) == 3)(serviceAppontment.Attributes.Contains("statecode")) && ((OptionSetValue)(serviceAppontment.Attributes["statecode"]).Value == 3),因爲字段「statecode」是一個OptionSet。還要替換||通過& &,因爲當serviceAppontment.Attributes.Contains("statecode"))爲false時,((OptionSetValue)(serviceAppontment.Attributes["statecode"]).Value會拋出一個NullReferenceException。

要更新現有約會,看起來好像在預約實體中查找serviceappointment實體。因此,您必須檢索與服務約定相關的所有約會。您可以使用此查詢:

QueryExpression queryExp = new QueryExpression 
{ 
    EntityName = "appointment", 
    ColumnSet = new ColumnSet("subject", "description", "scheduledstart", "scheduledend"), 
    Criteria = new FilterExpression 
    { 
     Conditions = { new ConditionExpression("el_serviceappointment_id", ConditionOperator.Equal, serviceAppontment.Id) } 
    } 

}; 

EntityCollection collection = obj.OrganizationService.RetrieveMultiple(queryExp); 

if (collection.Entities.Count > 0) 
{ 
    // now that you have all the appointments related with the serviceappoitement 
    // you can update de any appointment you want 
    obj.OrganizationService.Update(appointment); 
} 
+0

非常感謝。現在我需要更新我的記錄。我需要從serviceappoitnment中的另一個查找字段的字段中插入一個值來查找字段所需的出席者。你能幫我怎麼做嗎? – userS 2014-10-21 03:06:55

0

要在查找字段中serviceappoitnment的值更新記錄可檢索serviceappoitment記錄Entity svcApp = obj.OrganizationService.Retrieve(serviceAppontment.LogicalName, serviceAppontment.Id, new ColumnSet("<lookup_field_in_serviceappoitnment>");

您需要添加到collumnset查詢表達式屬性然後更新應用