check if opportunity has quotes on Won message with plugin RRS feed

  • Question

  • Hi

    I want to register a plugin for Opportunity Won message, and if the correspnding Opportunity has at least one quote with "X" status, then user can mark the Opportunity  as won, else he/she should not be able to save it .

    Would you help me please ?

    Tuesday, February 4, 2014 10:28 AM

All replies

  • Hi,

    Here is the step that you need to use

    > Write a plugin on Win message.

    > Get opportunityid from opportunity close entity parameter that you will get from inputparameter

    > Query quote entity based on opportunity id and check for quote status

    > Throw invalid plugin exception


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, February 5, 2014 6:37 AM
  • Hi remis,

     Register the following plugin code in Opportunity entity WIN message

    //Register your plugin in message: WIN primary entity: Opportunity Event: Pre-validation or pre-operation

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Metadata;

    using Microsoft.Xrm.Sdk.Query;


    namespace OpportunityWon


        public class Oppor_Quote:IPlugin


            public void Execute(IServiceProvider serviceProvider)


                //throw new NotImplementedException();

                IPluginExecutionContext context=(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));


                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                ITracingService tracingServices=(ITracingService)serviceProvider.GetService(typeof(ITracingService));










                    Entity entity = (Entity)context.InputParameters["OpportunityClose"];

                    //retrieve the opportunity id

                    EntityReference opportunityid = entity.GetAttributeValue<EntityReference>("opportunityid");


                    Guid opportunity = opportunityid.Id;


                    //retrieve all the Quotes is =won(2) for particular opportunity

    //here I retrieved all the quotes for corresponding opportunity which contains Status= Won

    //you can change based on your requirement like Active,closed..etc

                  string fetch=@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

                                 <entity name='quote'>

                                 <attribute name='name' />

                                 <attribute name='customerid' />

                                 <attribute name='statecode' />

                                 <attribute name='totalamount' />

                                 <attribute name='quoteid' />

                                 <attribute name='createdon' />

                                 <order attribute='name' descending='false' />

                                 <filter type='and'>

                                 <condition attribute='opportunityid' operator='eq' uitype='opportunity' value='{0}' />

                                 <condition attribute='statecode' operator='eq' value='2' />





                  string fetchexp = string.Format(fetch, opportunity);


                  EntityCollection collections = service.RetrieveMultiple(new FetchExpression(fetchexp));

                    //retrieve the counts of retrieved records

                  int relatedQuotesCount = collections.Entities.Count;

                    //check if th count is <=0(zoro or less than zero throw error  else make a opportunity as WIN)



                    throw new InvalidPluginExecutionException("Atleast one Quote should created for this opportunity with WON status to close this Opportunity as Won");




                catch (Exception ex)


                    throw new InvalidPluginExecutionException(ex.Message);






    Plugin Registration Steps:

    Message: WIN

    Primary Entity: Opportunity

    Event Pipeline Stage Execution: Pre-Operation

    Execution Mode: Synchronous


    Gugan A

    • Proposed as answer by Gugan A Wednesday, February 5, 2014 12:16 PM
    Wednesday, February 5, 2014 12:16 PM