Auto Create Price List Item for Dynamics CRM 2011 Products

Paul Nieuwelaar, 29 October 2012

The following plugin can be registered on post-create of the Product entity, so that each time a Product is created in your Dynamics CRM 2011 system, a Price List Item is automatically generated for that product.

The Price List Item will include the List Price defined on the Product. If you only have one Price List in your system, the Price List Item will be linked to that Price List. If you have multiple Price Lists, the first Price List created in your system will be used.

The Default Unit defined on the Product will be used as the Unit on the Price List Item. If no List Price is defined on the Product, the Price List Item will still be created, but the price will be $0.00, which you can modify later.

The full plugin is as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

    /// <summary>
    /// When a Product is created, if there is a Price List in the system automatically create a Price List Item
    /// plugin steps: product - create - post - sync
    /// </summary>
    public class Plugin : IPlugin
        public void Execute(IServiceProvider serviceProvider)
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            Entity target = context.InputParameters["Target"] as Entity;
            CreatePriceListItem(target, service);
        private void CreatePriceListItem(Entity product, IOrganizationService service)
            Money listPrice = new Money(0); //get the list price, or set to 0 if not set
            if (product.Attributes.Contains("price"))
                listPrice = (Money)product.Attributes["price"];

            EntityReference defaultUnit = (EntityReference)product.Attributes["defaultuomid"];           

            QueryExpression query = new QueryExpression { EntityName = "pricelevel" };
            query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
            query.ColumnSet = new ColumnSet("pricelevelid");
            query.Orders.Add(new OrderExpression("createdon", OrderType.Ascending));

            EntityCollection priceLists = service.RetrieveMultiple(query);
            if (priceLists.Entities.Count > 0)
                Entity firstPriceList = priceLists.Entities[0]; //get the first pricelist

                Entity priceListItem = new Entity("productpricelevel");
                priceListItem["amount"] = listPrice;
                priceListItem["uomid"] = defaultUnit;
                priceListItem["productid"] = new EntityReference("product", product.Id);
                priceListItem["pricelevelid"] = new EntityReference("pricelevel", firstPriceList.Id);
                service.Create(priceListItem); //create the price list item