更新插件,我需要編寫插件在CRM 2013是做兩兩件事:創建和CRM 2013的C#
- 如果statecode = 3和外地el_meeting_in_outlook_id是空的我 需要創建一個新的appoitment。
- 如果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
}
}
}
}
}
我不知道在一個應該更新預約第二部分寫。我試圖搜索網頁,但沒有成功。
你能幫忙嗎?
我可以問你爲什麼在POST操作中註冊了插件嗎?它會在相同的實體記錄上兩次導致UPDATE。第一次在實際過程中,第二次通過您的插件觸發UPDATE。它在PRE操作上註冊您的插件,在給定的情況下更適合。另一方面,PRE插件只會更新一次。 – Scorpion 2014-10-20 15:01:45
好吧,這是需求任務。我會和我的經理覈對一下。謝謝! – userS 2014-10-21 03:04:17