My first thought was that this could be on using the following Plugin setup:
- Message - Update
- Primary Entity - opportunity
- Secondary Entity - none
However I found that the above did not fire. A bit of googling and I found that i instead needed to use the following:
- Message - Win
- Primary Entity - opportunity
- Secondary Entity - none
AND
- Message - Lose
- Primary Entity - opportunity
- Secondary Entity - none
This does not return the opportunity as "Target" as you might think it actually returns "Status" and "OpportunityClose". OpportunityClose contains the information listed on the form displayed when you try to Win/Lose an Opportunity, CRM stores this as a separate entity.
Opportunity does provide you with the related opportunityid however so you can update the original opportunity if required (Just be aware that THIS will then fire any "Update opportunity" plugin steps!)
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof (IPluginExecutionContext)); if (context.InputParameters.Contains("OpportunityClose") && context.InputParameters["OpportunityClose"] is Entity) { // Obtain the target entity from the input parameters. var entity = (Entity)context.InputParameters["OpportunityClose"]; try { if (entity.Attributes.Contains("opportunityid")) { var opportunityId = ((EntityReference)entity.Attributes["opportunityid"]).Id; var serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory)); // Getting the service from the Organisation Service. IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); Entity opportunity = service.Retrieve("opportunity", opportunityId, new ColumnSet(new string[] { "closeprobability" })); opportunity["closeprobability"] = 50; service.Update(opportunity); } } catch (Exception ex) { tracingService.Trace("Plugin: {0}", ex.ToString()); throw; } }