If you’ve recently updated to Dynamics CRM 2015, there’s a chance that you ran into some unexpected problems with your old plugins. I’m going to write about a real scenario that happened to me the other day. When closing an opportunity as lost, users would see this error:
After downloading the log file, I was given a hint as to where to find the problem.
“This message cannot be used to set the state of opportunity to lost. In order to set state of opportunity to Lost, use the LoseOpportunityRequest message instead.”
You’ll see at the bottom (highlighted in yellow) that the error message also tells us exactly what class (Init), and what plugin (PopulatePriceList) the error has occurred in. This is a very good start to begin troubleshooting the error.
The next step in this scenario is to fire up Visual Studio and open the plugin in question.
After opening the solution, I noticed that the references were not setup correctly. At this point I decided to move forward with not only fixing the problem, but with updating this solution to use the latest Dynamics CRM SDK and a later version of .NET
You can get the latest version of the Dynamics CRM SDK from https://msdn.microsoft.com/en-us/library/hh547453.aspx
To fix the reference problem, I simply removed the old references, then re-added them from the BIN folder of the latest SDK.
Because I’m using the latest SDK, I also need to update this plugin’s reference to the .NET framework from 4.0 to 4.5.2, which you can accomplish by going to the project properties like shown in the screenshot below.
With all of that done, the project now builds correctly, and it’s time to begin troubleshooting the actual problem.
If you recall the error message from earlier, “This message cannot be used to set the state of opportunity to lost. In order to set state of opportunity to Lost, use the LoseOpportunityRequest message instead, we have a hint about looking for a message that’s probably deprecated now.
After looking through the plugin code, I find the prime suspect as shown in the screenshot below.
If (context.MessageName.Equals(“update” …)
The correct next step would probably be to start debugging this, however, I had a pretty good guess on what was happening here. The previous developer was saying that if the MessageName is Update, then update the entity. Because I set all this up dynamically in the plugin registration tool, which you can see below, I simply removed that condition and executed service.Update(target) and let the plugin registration tool dictate when and why it runs.
After updating the code, I built the solution and all seems like it should work.
To test this plugin, We have to run the plugin registration tool, which can be found in the Tools folder of the SDK you downloaded earlier. I won’t be diving into the plugin registration tool in this blog post, but I will do a follow up post soon detailing exactly what we can do with the plugin registration tool.
At a very high level, however, what we need to do now is register the assembly (PopulatePriceList), with the CRM. Afterwards, we need to create a step that will execute the plugin when an opportunity record is Updated. Below you can see a screenshot showing what that looks like.
That’s it! We just took an old plugin and got it ready for Dynamics CRM 2015 Update 1. If you have any questions please comment below. I’ll do everything I can to help out!
Keep an eye out for a blog post that gets really indepth with the plugin registration tool from me soon.