Softstuff Consulting
My occasional musings of a technical nature
  • Send us a message
  • About Softstuff Consulting
  • Privacy Policy
  • Main Website
C# , Dynamics365

Building a plugin for CRM 2015 Won or Lost Opportunities

by Ian Blair February 23, 2019 No Comments

Recently I had to develop a plugin for a CRM 2015 system that triggered each time the opportunity was modified. This worked fine for the normal day to day edits of the opportunity but it failed to catch when the opportunity was closed as Won or Lost.

Normally I would hook the plugin to the Update message on opportunity with the Plugin Registration Tool but when I tested it I wasn’t getting the results I wanted. After some digging around I discovered 2 more messages for opportunities, and these are Win and Lose.

On the face of it this seemed quite simple, just hook the plugin to these messages and its job done. Unfortunately its not quite as simple as that as the messages themselves don’t actually pass the opportunity entity when they fire, they actually pass the opportunityclose entity which I have to be honest I am not incredibly familiar with. The good news is I didn’t really have to worry about it as one of the attributes of the entity is the opportunityid, so all I had to do was get the passed opportunityclose entity and read the opportunityid attribute then search for the opportunity record with a matching id and then use those values. In fact it sounds more difficult than it actually is. 

Here is some sample code to catch the Win message

public void Execute(IServiceProviderserviceProvider)
{
    Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
    IOrganizationServiceFactoryserviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
    if (context.InputParameters.Contains("OpportunityClose"))
    {
        Entity oppCloseEnt = (Entity)context.InputParameters["OpportunityClose"];
        if (oppCloseEnt.Contains("opportunityid"))
        {
            EntityReference er = (EntityReference)oppCloseEnt["opportunityid"];
            // do stuff here
        }
    }
}

The good news is that the code is the same for the Lose message.

code

  • Previous Writing data to a Won Opportunity6 years ago
  • Next Preventing infinite loops in CRM2013/2015/2016 plugins6 years ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to make your Powershell scripts more annoying
  • What motherboard is in my PC
  • A Dynamics365 plugin thought experiment
  • Registering a Dynamics365 plugin and I get an error
  • Going back in time with Dynamics365

Categories

  • Bootstrap
  • C#
  • CSS
  • Dot Net Core
  • Dynamics365
  • JavaScript
  • Powershell
  • T-SQL
  • Thoughts
  • VBScript
  • Visual Studio
  • Windows
  • Xamarin

Recent Comments

  • S0ftStuffB055 on Call a Dynamics365 workflow from JavaScript
  • Siva on Call a Dynamics365 workflow from JavaScript
  • TC Sharpe on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • BigOwl on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • CRMGod on Access a Dynamics365 embedded webresource

Archives

  • January 2021
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • June 2019
  • May 2019
  • February 2019
2025 Softstuff Consulting. Donna Theme powered by WordPress
  • Twitter