我需要在CRM 2013中编写插件来做两件事:
这就是我写的:
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 :(得分:0)
在您定义已注册事件的位置,您必须将&#34; serviceappointment&#34;,即实体逻辑名称,而不是null。
然后将(serviceAppontment.Attributes.Contains("statecode")) || ((int)(serviceAppontment.Attributes["statecode"]) == 3)
替换为(serviceAppontment.Attributes.Contains("statecode")) && ((OptionSetValue)(serviceAppontment.Attributes["statecode"]).Value == 3)
,因为字段&#34;州代码&#34;是一个OptionSet。也替换|| by&amp;&amp;,因为当serviceAppontment.Attributes.Contains("statecode"))
为false时,((OptionSetValue)(serviceAppontment.Attributes["statecode"]).Value
将抛出NullReferenceException。
要更新现有约会,看起来在约会实体中有一个查找到serviceappointment实体。因此,您必须检索与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);
}
答案 1 :(得分:0)
使用serviceappoitnment中查找字段中的值更新记录,以便检索serviceappoitment记录Entity svcApp = obj.OrganizationService.Retrieve(serviceAppontment.LogicalName, serviceAppontment.Id, new ColumnSet("<lookup_field_in_serviceappoitnment>");
您需要在查询表达式中向collumnset添加属性requiredattendees,然后更新该appoitment
if(appointment.Attributes.Contains("requiredattendees") == true)
{
appointment.Attributes["requiredattendees"] = svcApp.Attributes["<lookup_field_in_serviceappoitnment>"];
}
else
{
appointment.Attributes.Add("requiredattendees", svcApp.Attributes["<lookup_field_in_serviceappoitnment>");
}