I am attempting to create a plugin that will set the Quantity of Order Products to 1 on initial create of an order. The reason I need to do this is the end users attach Opportunity Products to an Opportunity, then create an order from the Opportunity for "Free" copies of a product and the client doesn't want to give away many free copies by mistake.
The problem I have is that the plugin does not fire when I register it for Create on salesorderdetail (pre operation). Is there something simple I'm doing wrong?
My code is:
using System; using System.Collections.Generic; using System.Linq; using System.Text; // Microsoft Dynamics CRM namespace(s) using Microsoft.Xrm.Sdk; using System.ServiceModel; namespace CRMPlugin1 { public class SetOrderProductQty : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parmameters. Entity entity = (Entity)context.InputParameters["Target"]; try { if (entity.Attributes.Contains("quantity") == true) { entity.Attributes["quantity"] = 1; } else { entity.Attributes.Add("quantity", 1); } } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex); } } } }//end class }//end name space