The “IPluginExecutionContext” contains a property called “MessageName.” This is the name of the message that was called to perform the action. Here is a link to a list of messages: https://msdn.microsoft.com/en-us/library/gg309482.aspx . I couldn’t find my original source for this, so you can assume that each message name is just the class name listed without the request at the end. The one we are interested in is the “ConvertQuotetoSalesOrderRequest” or the “ConvertQuoteToSalesOrder” message. This is what is called when you click the “Create Order” button and can be called through the API as well. So to figure out if that is what is creating our sales order we would simply need to traverse the parent context list and check to see if the “MessageName” property equals “ConvertQuoteToSalesOrder”. Here is a code snippet to do that.
for (var parentContext = context.ParentContext; parentContext != null; parentContext = parentContext.ParentContext)
if (string.Equals(parentContext.MessageName, “convertquotetosalesorder”, StringComparison.InvariantCultureIgnoreCase))
You can see that we are traversing through the parent context’s checking to see if the “MessageName” property equals “ConvertQuoteToSalesOrder.” If it is, then we just return, which stops the plugin from executing. Immediately following, is where you would place the exception from above that explains to the user they must go through the quote to sales order process. Now that we have modified the plugin this last time, the business requirements are met. The only way the system will allow the creation of a sales order is if it is initiated after the “ConvertQuoteToSalesOrder,” which means it would have to follow the quote to sales order process.
If you would like to download a sample project that shows this solution in action, it can be found under my GitHub account at https://github.com/alally/CrmPluginParentContextBlogPost. All you need to do is download, build, and deploy. It will stop you from creating the sales order directly, but allow a converted quote to go through just fine. I’ve also added some logic that utilizes the “IPluginExecutionContext” to validate that the plugin is deployed correctly.
|ANDREW LALLY |Business Solutions Developer
A 2014 graduate of the University of Maryland, Andrew Lally received a B.S. in Computer Science along with a minor in Astronomy. His lack of experience in the corporate world does not translate into his experience and knowledge in software development. While attending school full time, Andrew was thrust into many different projects as a KTL part timeprogrammer. This gave him experience broadened his knowledge in many different areas. His experience includes working on projects in which he was to define his own programming language and create a compiler for it; programming a robot navigation algorithm that used only a camera and squares on the ground; Creating a smart task manager application for android and web devices that took into account where you are, what you were previously doing, and your preference doing certain activities in order to schedule tasks more efficiently. With Andrew’s determination, growing knowledge, and great work ethic, he has become a valuable team player within the KTL development team.