2017-09-27 3 views
0

保存する前に引用符を検証するプラグインを作成しています。私たちは引用符は、アクティブに勝ったり失われる可能性が前に引用符で引用製品ラインInvalidPluginExecutionExceptionで引用符を閉じないようにする検証プラグイン

項目がなければならないことを強制します。ドラフトモードにはこの要件はありません。

私は次のコードを書いています。リボンの「引用符を閉じる」ボタンを押して理由を選択すると、エラーメッセージが表示されるビジネスプロセスのエラーボックスが表示されます。

ただし、エラーメッセージを閉じてページを更新すると、引用符が閉じに設定されています。例外がスローされたにもかかわらず、見積もりを閉じるのはなぜですか?

FYIでは、プラグインステージがPre-operationに設定されています。ここで

は、私のソースコードは(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

NOTICE:株価がアクティブになって、そこには引用符の詳細がありませんので、引用符は優勝することができません。

Exception is successfully shown.

それがあるべきようなビジネス・プロセスのエラーが表示されます。ただし、ページを更新すると、見積もりステータスが「閉鎖」に設定されています。なぜ例外がスローされたのですか?

Quote is closed.

答えて

1

あなたは両方SetStateSetStateDynamicEntityメッセージ用のプラグインステップを登録する必要があります。

reference

のはなぜ 別途SetState関数とSetStateDynamicEntityに登録する必要がありますか?
前述のとおり、 がCRMで同じ処理を実行するという複数のメッセージがあります。このような例の1つは、SetStateRequest とSetStateDyanmicEntityRequestです。 SetStateにプラグインを書きたい場合、両方のメッセージにそれを登録する必要があります。

Read more

+0

ご回答ありがとうございました!上記の私の更新された質問を見てください。私はstepstateを追加しましたが、私はまだ同じ問題を抱えています。 – MasterProgrammer200

+1

「見積もりを変更しない」をチェックし、その動作を確認しましたか?Bcoz CRMは、「改訂見積もりを作成する」と言うと、新しいバージョンの作成を継続します –

+1

改訂見積もりが選択されているためではありませんでした。デバッグの後、私は見積もりを閉じるとメッセージがプラグインが閉じる。 Closeメッセージには、EntityMoniker EntityReferenceではなくQuoteClose Entityが含まれています。したがって、if(!isCorrectEvent ||!hasEnityMoniker)が返ります。 EntityMonikerがないのでプラグインを終了しました。私は一度私はそれを掃除が終了したが、問題が解決された私のコードを更新します。どうもありがとうございます! – MasterProgrammer200

関連する問題