我正在編寫一個插件來驗證報價,然後保存。我們要強制執行,必須有一個報價產品線引用驗證插件不會阻止關閉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單獨的步驟和更新我的源代碼。
然而,我仍然有同樣的問題。當我關閉報價時出現錯誤,但當我刷新頁面時,報價已設置爲關閉。
注意:行情活躍,沒有報價的細節,所以報價就不可能取得勝利。
顯示業務流程的錯誤,因爲它應該是。但是,當我刷新頁面時,報價狀態已設置爲「已關閉」。如果拋出異常,爲什麼會這樣做?
感謝您的答覆!請看看我上面更新的問題。我添加了setstatestate,但我仍然遇到同樣的問題。 – MasterProgrammer200
您是否嘗試檢查「不要修改報價單」並查看行爲?Bcoz CRM將繼續創建新版本,如果你說「創建一個修訂報價」 –
這不是因爲修改後的報價被選中......經過一些調試後,我能夠意識到,在關閉報價時,發送給插件是關閉。 Close消息包含一個QuoteClose實體,而不是EntityMoniker EntityReference。因此if(!isCorrectEvent ||!hasEnityMoniker)返回;因爲沒有EntityMoniker,所以退出了插件。一旦清理完成,我會更新我的代碼,但問題已解決。非常感謝! – MasterProgrammer200