locked
CRM 2011: Executing a plugin on opportunity when revising related quote RRS feed

  • Question

  • Hi all,

    I have developed a plugin that trigger the SetStateDynamicEntity message in opportunity to perform a specific action when the opportunity is reopened.

    This plugin is triggered only when the opportunity is re-opened directly, i.e using the Reopen Opportunity option in Ribbon, my need is to trigger it only when the opportunity is re-opened through revising the related quote.

     

    Please advise...

    Saturday, November 26, 2011 10:42 PM

Answers

  • Oh I understood

     

    Can we try to register this plugin on Post update, and filter the status column

     

     


    Many Thanks -Bhautik Desai xRM Technologies
    Sunday, November 27, 2011 12:49 PM
  • Hi Mostafa

    Sorry for Late reply

    I am on UK time and i was somewhere today wholeday

    "the given key was not present in the dictionary" means we are trying to access attribute which is not availble

    Can you please debug this plugin, i dont remember the attribute name, but I think, once you debug then you will see all available Attributes and associated values

    and Alsp please keep the condition for each attributes

     

    Like

    if(preOpen.Contains("statecode"))

    OptionSetValue preStateCode = (OptionSetValue)preOpen["statecode"];
                            int preStatus = preStateCode.Value;

    }

     

    Just move the mouse to Entity preOpen statement, you will see all attribute in debug session.

     

    I will also try to find exact attribute.

     

    Thank you

     

     


    Many Thanks -Bhautik Desai xRM Technologies
    Monday, November 28, 2011 5:39 PM

All replies

  • Hi,

    I think we can handle this situation manually.

    Actually when opportunity status changed by Quotes, then actually first Quote record gets updated and then Opportunity status gets changed

    (1)    We can add one hidden flag (Boolean attribute) to Opportunity which will be ‘false’ bydefault.

    (2)    We write one plugin on Quotes status changed, which will update this flag to ‘true’ for associated Opportunity.

    (3)    Let existing plugin trigger every time when on status change of opportunity.(by Ribbon button or by Quotes) (So we need to configure this plugin such a way that it should trigger every time when opportunity status change)

    (4)    In plugin logic check that flag, if it is true then only your logic should fire. (Means if that flag value is ‘true’ then consider as that trigger is generated from Quotes not from Button on Opportunity)


    Many Thanks -Bhautik Desai xRM Technologies
    Sunday, November 27, 2011 1:03 AM
  • (3)    Let existing plugin trigger every time when on status change of opportunity.(by Ribbon button or by Quotes) (So we need to configure this plugin such a way that it should trigger every time when opportunity status change

    And this is the main problem how to do this ??

    Regarding the below Hello World plugin sample:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System.ServiceModel;
    using Microsoft.Crm.Sdk.Messages;
    
    namespace CloseReopenedOpportunity
    {
        public class CloseOpportunity : IPlugin
        {
            public void Execute( IServiceProvider serviceProvider )
            {
                try
                {
                    // Obtain the execution context from the service provider.
                    IPluginExecutionContext context = (IPluginExecutionContext)
                        serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                         throw new InvalidPluginExecutionException("HELLO");
                }
                catch ( Exception e )
                {
                    throw new InvalidPluginExecutionException(e.Message, e);
                }
            }
        }
    }
    

    It has been registered against the SetStateDynamicEntity message on post operation for opportunity

    but It triggers only when opportunity is re-opened directly

    Sunday, November 27, 2011 12:28 PM
  • Oh I understood

     

    Can we try to register this plugin on Post update, and filter the status column

     

     


    Many Thanks -Bhautik Desai xRM Technologies
    Sunday, November 27, 2011 12:49 PM
  • It works, but when I add the following statement:

    int postStatus = ((OptionSetValue)context.InputParameters["State"]).Value;

    it through "the given key was not present in the dictionary" exception

    Please advise on how to get the status value in this case ??

    Sunday, November 27, 2011 2:41 PM
  • Dear Bahautik, any suggestions ??

    Sunday, November 27, 2011 10:51 PM
  • Hi,

    Try registering your plugin on Revise message.

     


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, November 28, 2011 12:57 AM
  • I need to execute the plugin against the opportunity not the quote
    Monday, November 28, 2011 1:05 AM
  • what is the status of opportunity when the opportunity is re-opened through revising the related Quote,

    Did you try Win / Lose message of Opportunity entity.


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, November 28, 2011 1:32 AM
  • what is the status of opportunity when the opportunity is re-opened through revising the related Quote,

    Did you try Win / Lose message of Opportunity entity.

    The status of the re-opened opportunity is open, however I have tried the following and it works:

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                            Entity entity = (Entity)context.InputParameters["Target"];
    
                            int postStatus = -1;
                            if (entity.Attributes.Contains("statecode"))
                            {
                                postStatus = ((OptionSetValue)entity.Attributes["statecode"]).Value;
                                //throw new InvalidPluginExecutionException(postStatus.ToString());
                            }
    }
    

    But when I tried to access some pre values using a pre image as follow:

    Entity preOpen = (Entity)context.PreEntityImages["PreOpportunityImage"];
                            OptionSetValue preStateCode = (OptionSetValue)preOpen["statecode"];
                            int preStatus = preStateCode.Value;
    
                            OptionSetValue preStatusCode = (OptionSetValue)preOpen["statuscode"];
                            int preStatusReason = preStatusCode.Value;
    
                            Money preActualValue = (Money)preOpen["actualvalue"];
                            string preActualRevenue = preActualValue.Value.ToString();
    
                            DateTime preActualCloseDate = (DateTime)preOpen["actualclosedate"];
                            string actualCloseDate = preActualCloseDate.AddDays(1).ToShortDateString();
    
                            Guid keyOppId = (Guid)preOpen.Attributes["opportunityid"];
                            string oppId = keyOppId.ToString();
    


    I get "the given key was not present in the dictionary" exception

    " exception

    I have registered my plugin against the update message on post operation, filtering by statecode and pre image filtering by:both all fields or statecode, statuscode, actualvalue, actualclosedate, and opportunityid

    Please advise...

    Monday, November 28, 2011 4:53 PM
  • Hi Mostafa

    Sorry for Late reply

    I am on UK time and i was somewhere today wholeday

    "the given key was not present in the dictionary" means we are trying to access attribute which is not availble

    Can you please debug this plugin, i dont remember the attribute name, but I think, once you debug then you will see all available Attributes and associated values

    and Alsp please keep the condition for each attributes

     

    Like

    if(preOpen.Contains("statecode"))

    OptionSetValue preStateCode = (OptionSetValue)preOpen["statecode"];
                            int preStatus = preStateCode.Value;

    }

     

    Just move the mouse to Entity preOpen statement, you will see all attribute in debug session.

     

    I will also try to find exact attribute.

     

    Thank you

     

     


    Many Thanks -Bhautik Desai xRM Technologies
    Monday, November 28, 2011 5:39 PM
  • Hello Bhautik,

     

    thanks alot :), I have tried the following and it works for all fields except actual close date and actual revenue:

     

    int preStatus = -1;

                            int preStatusReason = -1;

                            string preActualRevenue = "";

                            string actualCloseDate = "";

                            string oppId = "";

     

                            Entity preOpen = (Entity)context.PreEntityImages["PreOpportunityImage"];

     

                            if (preOpen.Contains("statecode"))

                            {

     

                                OptionSetValue preStateCode = (OptionSetValue)preOpen["statecode"];

                                preStatus = preStateCode.Value;

     

                            }

     

                            if (preOpen.Contains("statuscode"))

                            {

                                OptionSetValue preStatusCode = (OptionSetValue)preOpen["statuscode"];

                                preStatusReason = preStatusCode.Value;

                            }

     

                            if (preOpen.Contains("actualvalue"))

                            {

                                Money preActualValue = (Money)preOpen["actualvalue"];

                                preActualRevenue = preActualValue.Value.ToString();

                            }

     

                            if (preOpen.Contains("actualclosedate"))

                            {

                                DateTime preActualCloseDate = (DateTime)preOpen["actualclosedate"];

                                actualCloseDate = preActualCloseDate.AddDays(1).ToShortDateString();

                            }

     

    throw new InvalidPluginExecutionException("opportunity data was \nStatus " + preStatus +

                                                                        "\nStatusReason " + preStatusReason +

                                                                        "\nActual Revenue " + preActualRevenue +

                                                                        "\nActual Close Date " + actualCloseDate +

                                                                        "\nStatus after re-open is " + postStatus);

     

    It displays null for both fields

    Monday, November 28, 2011 10:42 PM
  • hi

    if you can debug the code, then you should find allavailable attributes.

    some times, it's possible that we use different spelling of attribute.

    So if you stop at debug point and check all attributes in array, you should able to find attribute for close date and actual revenue

     

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    Many Thanks -Bhautik Desai xRM Technologies
    Monday, November 28, 2011 10:53 PM
  • Dear Bhautik,

    I have checked it and I have found the attributes named are correct, however I have tried to filter the step by statecode, actual close date and actual revenue and it works, but it didn't display the statecode default value

    Please advise

    Tuesday, November 29, 2011 12:22 PM
  • Any suggestions ??
    Tuesday, November 29, 2011 7:43 PM
  • Hi,

    Try registering your plugin on Revise message.

     


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    In 2011 there is no Revise message.  I am registered on Win, SetState and SetStateDynamicEntity.  When I reopen a won opportunity plugin does not fire.

    Why?

    Monday, July 22, 2013 2:33 PM