locked
PostOpportunityWin message - Context Input Parameters? RRS feed

  • Question

  • Hi, I'm fairly new to CRM development, but I have written a few simple plugins.  What I have now, I'm having trouble with one custom plugin I'm trying to create.  Basically I'm trying to run some code when an Opportunity is Closed as Won.  However, when I create my plugin registered as 'PostOpportunityWin' event my plugin wasn't doing anything (no errors, it's just not doing anything).  I've narrowed it down that it's not getting past the 'If' statement here:

    if (context.InputParameters.Contains("Target")
                && context.InputParameters["Target"] is Entity)

    Is this not the correct test for this message?  I've tried testing for InputParameters.Contains("EntityMoniker"), as well.  I also tested for EntityReference for both of these as well.  Here's the full block of code in my Plugin:

    using System;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Query;
    
    
        /// <summary>
        /// PostOpportunityWin Plugin.
        /// </summary>    
    public class PostOpportunityWin : Plugin
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostOpportunityWin"/> class.
        /// </summary>
        public PostOpportunityWin()
            : base(typeof(PostOpportunityWin))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Win", "opportunity", new Action<LocalPluginContext>(ExecutePostOpportunityWin)));
    
            // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
            // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
        }
    
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostOpportunityWin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }
    
    
    
            // Obtain the execution context from the service provider.
            IServiceProvider serviceProvider = localContext.ServiceProvider;
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
    
    
    
    
            if (context.InputParameters.Contains("Target")
                && context.InputParameters["Target"] is Entity)
            {
    
                // Create a task activity to follow up with the account customer in 7 days. 
    
                Entity followup = new Entity("task");
    
                followup["subject"] = "DEBUG TASK - IGNORE";
                followup["description"] = "WHY?";
                followup["scheduledstart"] = DateTime.Now.AddDays(7);
                followup["scheduledend"] = DateTime.Now.AddDays(7);
                followup["category"] = context.PrimaryEntityName;
    
                // Refer to the opportunity in the task activity.
                if (context.OutputParameters.Contains("id"))
                {
                    Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                    string regardingobjectidType = "opportunity";
    
                    followup["regardingobjectid"] =
                    new EntityReference(regardingobjectidType, regardingobjectid);
                }
    
                service.Create(followup);
            }
        }
    }

    If I put the code to create that task outside the IF ...the task gets created when the opportunity is won.  I'm not sure what I've got wrong here.  Any ideas?  This is CRM 2013 Online





    • Edited by John-G-M Thursday, January 22, 2015 10:34 PM
    Thursday, January 22, 2015 10:30 PM

Answers

All replies