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.
STOPPING A SALES ORDER FROM BEING CREATED
Stopping an operation using a plugin is a simple process. All you need to do is throw an exception, but its best to not just throw any exception. CRM provides the “InvalidPluginExectionException” that allows you to pass in a message that will get displayed to the user. Using that, we can alert the user that the action they took is not allowed and guide them in the direction of the correct way to create an order.
The customer wants to stop users and outside processes from being able to create a sales order directly in CRM. Instead, they have set up a requirement that all sales orders must be created through the sales process. They first create a quote and then use the CRM “Create Order” button on a submitted quote form to convert that quote into a sales order.