2014-10-20 59 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查询表达式属性然后更新应用