【问题标题】:dynamics crm plugin delete entity during update message在更新消息期间动态 crm 插件删除实体
【发布时间】:2016-01-06 14:33:16
【问题描述】:

是否可以在插件更新事务中删除实体?

以下代码似乎不起作用。我需要在实体更新和其他一些情况下删除实体

类似:

protected void ExecutePosAnnotationtUpdate(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    if (localContext.PluginExecutionContext.Depth > 1) return;


    Entity postEntityImage = null;
    if (localContext.PluginExecutionContext.PostEntityImages.Contains("PostImage"))
    {
        if (localContext.PluginExecutionContext.PostEntityImages["PostImage"] != null)
        {
            postEntityImage = localContext.PluginExecutionContext.PostEntityImages["PostImage"];
        }
    }

    Entity preEntityImage = null;
    if (localContext.PluginExecutionContext.PreEntityImages.Contains("PreImage"))
    {
        if (localContext.PluginExecutionContext.PreEntityImages["PreImage"] != null)
        {
            preEntityImage = localContext.PluginExecutionContext.PreEntityImages["PreImage"];
        }
    }

    if ((bool)postEntityImage.Attributes["isdocument"])
    {
        if ( some condition )
            localContext.OrganizationService.Delete(postEntityImage.LogicalName, postEntityImage.Id);
    }
}

`

【问题讨论】:

  • 您是否尝试在异步更新阶段注册您的插件?
  • 我很好奇:您是否试图阻止用户在便笺中上传文件?
  • @Alex :) 是的,几乎你是对的 .. 我想在更新事件中移动附件
  • 如果您在本地,您可以打开跟踪并查看更详细的错误以确定发生了什么。

标签: dynamics-crm-2011 dynamics-crm dynamics-crm-2013 dynamics-crm-online


【解决方案1】:

由于您正在更新,记录在Target 中。

public void Execute(IServiceProvider serviceProvider)
{
    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    var service = serviceFactory.CreateOrganizationService(context.UserId);

    var target = context.InputParameters["Target"] as Entity;
    var condition = /* whatever */ 
    if(condition)
    {
        service.Delete(target.LogicalName, target.Id);
    }
}

附加到Update 消息、Post-OperationAsynchronous 时按预期工作。也可以在Sandbox 内工作。

记录不会立即消失,这需要一些时间(在我的本地操场上大约 20 秒)。如果您将其设为Synchronous,它仍然可以工作,但会出现警报,因为数据在更新期间由 CRM 处理时会消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多