2017-09-27 57 views
0

我正在编写一个插件来验证报价,然后保存。我们要强制执行,必须有一个报价产品线引用验证插件不会阻止关闭InvalidPluginExecutionException引用

在报价前的报价项目可以激活,赢得或失去。草稿模式没有这个要求。

我编写了下面的代码,当按下功能区上的“Close Quote”按钮并选择Won作为原因时,弹出一个业务流程错误框,并显示错误消息。

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

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

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

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月2日:

我创建了SETSTATE单独的步骤和更新我的源代码。

SetState

然而,我仍然有同样的问题。当我关闭报价时出现错误,但当我刷新页面时,报价已设置为关闭。

Close Quote

注意:行情活跃,没有报价的细节,所以报价就不可能取得胜利。

Exception is successfully shown.

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

Quote is closed.

回答

1

您必须先注册两个SetStateSetStateDynamicEntity消息插件的步骤。

reference

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

Read more

+0

感谢您的答复!请看看我上面更新的问题。我添加了setstatestate,但我仍然遇到同样的问题。 – MasterProgrammer200

+1

您是否尝试检查“不要修改报价单”并查看行为?Bcoz CRM将继续创建新版本,如果你说“创建一个修订报价” –

+1

这不是因为修改后的报价被选中......经过一些调试后,我能够意识到,在关闭报价时,发送给插件是关闭。 Close消息包含一个QuoteClose实体,而不是EntityMoniker EntityReference。因此if(!isCorrectEvent ||!hasEnityMoniker)返回;因为没有EntityMoniker,所以退出了插件。一旦清理完成,我会更新我的代码,但问题已解决。非常感谢! – MasterProgrammer200