简体   繁体   中英

Prevent Delete - Using plugin in CRM 2011

How to prevent delete based on condition in a plugin in Microsoft Dynamics CRM 2011? I have tried InvalidPluginExecutionException but it is not working.

This is my code.

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

Can you please help me on what needs to be done?

You need to add a PreImage to the Plugin Registration for the field "new_hutang." The Target entity won't include all the fields on a delete by default, but the PreImage entity will if you specify it when you register your plugin. See this blog post for more information: https://deepakexploring.wordpress.com/2011/02/04/preentityimages-and-postentityimages-in-crm-5-0-2011/

Additional to @Josh Painter (which is the best approach) you can also use a javascript instruction to make sure that the field is included.

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

This line could be inside an onsave function that you would "attach" to the OnSave envent of the form. This would also ensure that the field exists in your plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM