我试图设置"描述" Post插件中Create事件中的Appointment实体上的字段。
这是CRM 2013 Online。
这是我遇到的错误
未处理的例外情况: System.ServiceModel.FaultException`1 [Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk,Version = 6.0.0.0,Culture = neutral, PublicKeyToken = 31bf3856ad364e35]]:System.ArgumentException:this 查找只能显示一个项目。查找ID =详细信息:
-2147220970 System.ArgumentException:此查找只能显示 一个项目。查找ID =
2014-04-25T11:35:30.0267579Z
此字段:
插件的注册方式
代码I使用:
using System;
using System.ServiceModel;
using System.Web.Services.Protocols;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System.Linq;
using System.Linq.Expressions;
namespace Itrim.Plugin
{
public class Appointment_SetDayName : IPlugin
{
private const string _EntityName = "appointment";
private bool IsContextValid(IPluginExecutionContext context)
{
if (
context.InputParameters.Contains(ParameterName.Target) &&
context.InputParameters[ParameterName.Target] is Entity &&
context.PrimaryEntityName == _EntityName)
{
return true;
}
return false;
}
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (!IsContextValid(context))
return;
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService organization = factory.CreateOrganizationService(context.UserId);
Entity target = (Entity)context.InputParameters[ParameterName.Target];
string descName = "description";
SetValue(target, descName, "test: " + DateTime.Now.ToString());
// if post, need to save the entity, if pre, then just set values.
organization.Update(target);
}
private static void SetValue(Entity entity, string fieldName, object value)
{
if (entity.Attributes.Contains(fieldName) == false)
entity.Attributes.Add(fieldName, value);
entity.Attributes[fieldName] = value;
}
}
}
答案 0 :(得分:1)
您不需要调用更新方法。解决问题的步骤是:
使用以下Execute方法重写插件代码:
IPluginExecutionContext context =(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if(!IsContextValid(context)) 返回;
实体目标=(实体)context.InputParameters [ParameterName.Target]; 目标["描述"] ="测试:" + DateTime.Now.ToString();