引用验证插件不会阻止在InvalidPluginExecutionException上关闭引用

时间:2017-09-27 20:13:18

标签: c# plugins dynamics-crm dynamics-crm-365

我正在编写插件以在保存之前验证报价。我们希望强制必须有报价产品线

报价前的报价项目可以激活,赢或输。草稿模式没有此要求。

我编写了以下代码,当按下功能区上的“关闭引用”按钮并选择Won作为原因时,会弹出一个错误消息的业务流程错误框。

但是,在关闭错误消息并刷新页面后,报价将设置为已关闭。为什么即使异常被抛出,报价也会关闭?

仅供参考,插件阶段设置为预操作。

这是我的源代码(2017年10月10日更新):

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValbrunaPlugins
{
    public class QuoteValidation : IPlugin
    {
        private ITracingService tracingService;

        public void Execute(IServiceProvider serviceProvider)
        {

            // retrieve the context, factory, and service
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            bool isCorrectEvent = context.MessageName == "SetStateDynamicEntity" || context.MessageName == "SetState" || context.MessageName == "Win" || context.MessageName == "Close";
            bool hasEnityMoniker = context.InputParameters.Contains("EntityMoniker");

            // ensure we are handling the correct event and we were passed an entity from the context
            if (!isCorrectEvent || !hasEnityMoniker) return;
            // get the reference to the quote entity
            EntityReference quoteEntityReference = (EntityReference)context.InputParameters["EntityMoniker"];

            Entity quoteEntity = null;
            try
            {
                // get the quote entity from the entity reference
                quoteEntity = ActualEntity.GetActualEntity(quoteEntityReference, service);
            } catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("Quote with id " + quoteEntityReference.Id + " not found.");
            }

            // ensure that we have the correct entity
            if (quoteEntity.LogicalName != "quote") return;

            // write query to retrieve all the details for this quote
            QueryExpression retrieveQuoteDetailsQuery = new QueryExpression
            {
                EntityName = "quotedetail",
                ColumnSet = new ColumnSet(),
                Criteria = new FilterExpression
                {
                    Conditions =
                        {
                            new ConditionExpression
                            {
                            AttributeName = "quoteid",
                            Operator = ConditionOperator.Equal,
                            Values = { (Guid)quoteEntity.Id }
                            }
                        }
                }
            };

            // execute the query to retrieve the details for this quote
            EntityCollection quoteDetails = service.RetrieveMultiple(retrieveQuoteDetailsQuery);

            // retrieve the current status of the quote
            // 0 - Draft
            // 1 - Active
            // 2 - Won
            // 3 - Closed
            int quoteStatus = ((OptionSetValue)(quoteEntity.Attributes["statecode"])).Value;

            // if not in draft mode
            if (quoteStatus != 0)
            {
                // if the amount of details for the quote is less than 1
                if (quoteDetails.Entities.Count < 1)
                {
                    throw new InvalidPluginExecutionException("There must be a quote product line item on a quote before a quote can be activated, won, or lost while not in draft mode.");
                }
            }
        }

    }
}

2017年10月10日更新:

我为SetState创建了一个单独的步骤并更新了我的源代码。

SetState

但是,我仍然遇到同样的问题。当我关闭报价时,我得到了错误,但是当我刷新页面时,报价已被设置为关闭。

Close Quote

注意:报价处于活动状态,没有报价详细信息,因此无法获得报价。

Exception is successfully shown.

应显示业务流程错误。但是,当我刷新页面时,报价的状态已设置为“已关闭”。如果抛出异常,为什么会这样做?

Quote is closed.

1 个答案:

答案 0 :(得分:1)

您必须为SetStateSetStateDynamicEntity消息注册插件步骤。

reference

  

为什么我需要在SetState和SetStateDynamicEntity上注册   分别?
  正如我前面提到的,有多条消息   在CRM中执行相同的操作。一个这样的例子是SetStateRequest   和SetStateDyanmicEntityRequest。如果你想写一个插件   SetState,然后你需要在两个消息上注册它。

Read more