防止删除 - 在 CRM 2011 中使用插件

Prevent Delete - Using plugin in CRM 2011

如何在 Microsoft Dynamics CRM 2011 的插件中防止基于条件的删除?我已经尝试 InvalidPluginExecutionException 但它不起作用。

这是我的代码。

try
{
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        Entity entity = (Entity)context.InputParameters["Target"];
        new_testing testing = entity.ToEntity<new_testing>();
        if (testing.new_hutang.Value == true)
        {
            throw new InvalidPluginExecutionException("error delete");
        }
    }
}
catch (InvalidPluginExecutionException e)
{
    Console.WriteLine("cancel delete plugin" + e);
}

你能帮我看看需要做什么吗?

您需要为该字段添加一个 PreImage 到插件注册 "new_hutang." 默认情况下,Target 实体不会包含删除时的所有字段,但如果您在删除时指定它,PreImage 实体会包含注册你的插件。有关详细信息,请参阅此博客 post:https://deepakexploring.wordpress.com/2011/02/04/preentityimages-and-postentityimages-in-crm-5-0-2011/

除了@Josh Painter(这是最好的方法)之外,您还可以使用 javascript 指令来确保包含该字段。

Xrm.Page.getAttribute("new_hutang").setSubmitMode("always");

这一行可以在一个 onsave 函数中,您可以 "attach" 到表单的 OnSave 事件。这也将确保该字段存在于您的插件中。