Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics 365: Quote Validation Plugin Not Preventing Closing of Quote on InvalidPluginExecutionException

(0) ShareShare
ReportReport
Posted on by Microsoft Employee
I am writing a Plugin to validate a quote before it saves. We want to make enforce that there must be a quote product line 
item on a quote before a quote can be activated, won or lost. I wrote the following code and when pressing the "Close Quote"
button on the ribbion and selecting "Won" as the reason, a business process error box pops up with the error message. However,
upon closing the error message and refreshing the page, the quote is set to closed. Why does it close even though the exception
was thrown?

FYI, the plugin stage is set to Pre-operation.

Here is my source code:

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); // ensure we are handling the correct event and we were passed an entity from the context if (context.MessageName != "SetStateDynamicEntity" || !context.InputParameters.Contains("EntityMoniker")) return; // get the reference to the quote entity EntityReference quoteEntityReference = (EntityReference)context.InputParameters["EntityMoniker"]; // get the quote entity from the entity reference Entity quoteEntity = ActualEntity.GetActualEntity(quoteEntityReference, service); // 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) { // iif 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."); } } } }

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics 365: Quote Validation Plugin Not Preventing Closing of Quote on InvalidPluginExecutionException

    A solution was posted here: stackoverflow.com/.../quote-validation-plugin-not-preventing-closing-of-quote-on-invalidpluginexecutio

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans