Tuesday 24 June 2014

Update Opportunity on 'Close as Won' or 'Close as Lost'

I had a requirement that I thought would be quite basic, on Close Won/Lost then update a field on Opportunity.

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;
 }
}

Sunday 22 June 2014

CRM 2013 Plugin Registration Tool - Cannot connect to Organisations after login

When attempting to connect to the Plugin Registration Tool I found a strange scenario.  If I entered the correct credentials the tool would look like it was doing something and then return to the login page. Clicking on the Login button again would flash to a message and come back.

CRM 2013 Plugin Registration Tool Login Page


We then added me as a System administrator and I could go one step further (Seeing a list of Organisations) yet if I clicked on one then the list disappeared and I was stuck!

CRM 2013 Plugin Registration Tool Organisation List

So I did the only other thing I could think of which was started Fiddler and see the messages between my system and CRM.

This is where I discovered that there was an issue with the SSL Certificate and rather than give me a nice message CRM just fell over.

So if you can't get past the Login or Organisation screen then check your certificate isn't throwing an error.

Finally the Plugin Registration Tool is new and improved!

If you're like me and find yourself often registering CRM plugins then you will probably be as excited as me that the Plugin Registration Tool has been reborn!

While the old Plugin Registration Tool did its job it always concerned me that such a key bit of Dynamics CRM functionality could be such and afterthought for Microsoft (No one had time to find an icon for it??)

When downloading the latest Dynamics CRM SDK 2013 I suddenly discovered the Plugin Registration Tool wasn't in its old spot. Its now found at {Base Dir}\SDK\Tools\PluginRegistration and it looks a fair bit different as shown below!

The Old


The New!
The functionality in general remains the same. Its just a fresher interface however there is a few changes both good and bad.

  1. You can now view the plugins by Assembly, Entity, or Message. This makes it a lot easier when searching for a particular plugin step.
  2. Currently it doesn't keep a list of servers/logins. Painful if you have a collection of servers!
  3. The source code isn't available in the SDK.
Whether or not the source code is ever made available will be a big question marks however hopefully we can get further improvements in the near future!