Crm插件更新失败

时间:2016-12-05 12:55:40

标签: plugins dynamics-crm crm

我创建了两个名为" Price"对于报价和报价产品,我想在每次更新第一个时更新第二个。

这是我的代码:

protected void ExecutePostAccountUpdateContacts(LocalPluginContext localContext)
{
if (localContext == null)
{
    throw new ArgumentNullException("localContext");
}
string oldPrice = ""; 
string newPrice = ""; 

IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;

var ServiceContext = new OrganizationServiceContext(service);
ITracingService tracingService = localContext.TracingService;

if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
    Entity entity = (Entity)context.InputParameters["Target"];

    Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;

    // get the post entity image
    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

    if (preImageEntity.Attributes.Contains("Price"))
    {
        oldPrice = (string)preImageEntity.Attributes["Price"];
    }

    if (postImageEntity.Attributes.Contains("Price"))
    {
        newPrice = (string)postImageEntity.Attributes["Price"];
    }

    if (newPrice != oldPrice)
    {
        try
        {
            //Create query to get the related contacts
            var res = from c in ServiceContext.CreateQuery("Products")
                        where c["parentQuoteid"].Equals(entity.Id)
                        select c;

            foreach (var c in res)
            {
                Entity e = (Entity)c;
                e["Price"] = newPrice;

                ServiceContext.UpdateObject(e);
            }

            ServiceContext.SaveChanges();
        }
        catch (FaultException ex)
        {
            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

虽然您没有提出问题,但您的查询并不完全正确。因此,我假设您使用product查询parentquoteid时插件失败。

并非所有linq运算符都已实现,也将实体逻辑名称作为参数传递给create查询,因​​此Products只代替product而不是parentquoteid。没有名为var res = from c in ServiceContext.CreateQuery("product") where c.GetAttributeValue<Guid>("new_parentquoteid") == entity.Id select c; 的开箱即用字段,您是否缺少自定义属性前缀?

{{1}}