【发布时间】:2015-10-15 21:20:18
【问题描述】:
在内部部署的 CRM 2013 上,我正在尝试编写一个插件,该插件会在对 Quote 上的字段进行更新时触发。然后插件创建一个新的自定义实体“new_contract”。
当对该字段进行更新时,我的插件被成功触发。但是,在尝试创建新的自定义实体时,我不断收到错误消息“字典中不存在给定的键”。
我在这段代码中使用了“PostImage”。我确认它在插件注册中使用相同的名称注册。
这里是代码
var targetEntity = context.GetParameterCollection<Entity>
(context.InputParameters, "Target");
if (targetEntity == null)
{throw new InvalidPluginExecutionException(OperationStatus.Failed,
"Target Entity cannot be null")}
var postImage = context.PostEntityImages["PostImage"];
if (postImage == null)
{throw new InvalidPluginExecutionException(OperationStatus.Failed,
"Post Image is required");}
var quote = context.GenerateCompositeEntity(targetEntity, postImage);
//throw new InvalidPluginExecutionException(OperationStatus.Failed, "Update is captured");
//Guid QuoteId = (Guid)quote.Attributes["quoteid"];
var serviceFactory = (IOrganizationServiceFactory)serviceProvider
.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
var contractEntity = new Entity();
contractEntity = new Entity("new_contract");
if (quote.Attributes.Contains("portfolio"))
{
var quotePortfolio = (EntityReference)quote.Attributes["new_portfolio];
contractEntity[Schema.new_contract.PortfolioName] =
new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
}
if (quote.Attributes.Contains(Schema.Quote.QuoteName))
{
var quoteName = (string)quote.Attributes["name"];
contractEntity[Schema.new_contract.contractName] = quoteName;
}
var contractId = service.Create(contractEntity);
【问题讨论】:
-
显示您的完整方法。您在声明中使用
var,因此您发布的内容中每种类型都不清楚。
标签: plugins crm dynamics-crm-2013