Dynamics 365 CE在Pre Delete插件中应用Image

微软动态CRM专家罗勇 ,回复327或者20190428可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!

在插件中限制记录的删除是常见的场景,比如根据statuscode的状态来限制删除。

这时候在实体的Delete消息的Pre Operation阶段注册一个插件,并注册一个Image,我这里类似如下:

 

 

然后我这里使用插件代码类似如下:

using Microsoft.Xrm.Sdk;
using System;

namespace Plugins
{
    public class PreMyEntityDelete : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if(context.PreEntityImages.Contains("PreImg"))
            {
                var statuscodeField = context.PreEntityImages["PreImg"].GetAttributeValue<OptionSetValue>("statuscode");
                if(statuscodeField.Value != 1 && statuscodeField.Value != 2)
                {
                    throw new InvalidPluginExecutionException("Only draft or inactive request can be deleted!");
                }
            }
            else
            {
                throw new InvalidPluginExecutionException("Pre delete image - PreImg does not exist!");
            }
        }
    }
}

 

posted @ 2019-04-28 23:23  微软MVP(15-18)罗勇  阅读(442)  评论(0编辑  收藏  举报