【发布时间】:2014-10-20 06:21:23
【问题描述】:
我需要在 CRM 2013 中编写插件来做两件事:
- 如果 statecode = 3 并且字段 el_meeting_in_outlook_id 为空 I 需要创建一个新的预约。
- 如果 statecode = 3 并且字段 el_meeting_in_outlook_id 不为空 我需要更新现有的预约。
这是我写的:
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
}
}
}
}
}
我不知道在应该更新约会的第二部分中写什么。我试图搜索网络但没有成功。
你能帮忙吗?
【问题讨论】:
-
请问您为什么在POST操作中注册了插件?它将在同一实体记录上导致两次更新。第一次在实际过程中,第二次通过您的插件触发更新。如果您在 PRE Operation 上注册您的插件,这将更适合给定场景。另一方面,PRE 插件只会更新一次。
-
嗯,这是需求任务。我会与我的经理核实。谢谢!
标签: plugins dynamics-crm-2011 crm