动态crm插件在更新消息期间删除实体

时间:2015-10-08 19:51:27

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

是否可以删除实体,同时仍然在插件更新事务中?

以下代码似乎无效。我需要在更新实体和其他一些情况时删除实体

类似的东西:

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);
    }
}

`

1 个答案:

答案 0 :(得分: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处理时消失。