Auto Create Price List Items for Dynamics CRM 2011 Products

Posted: December 5, 2012 in Dynamics CRM 2011

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;

namespace Tester.Plugin
    /// When a Product is created, if there are Price Lists in the system automatically create a Price List Item for each
    /// plugin steps: product - create - post - sync
    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)
                priceLists.Entities.ToList().ForEach(priceList =>
                    Entity priceListItem = new Entity("productpricelevel");
                    priceListItem["amount"] = listPrice;
                    priceListItem["uomid"] = defaultUnit;
                    priceListItem["productid"] = new EntityReference("product", product.Id);
                    priceListItem["pricelevelid"] = new EntityReference("pricelevel", priceList.Id);

                    service.Create(priceListItem); //create the price list item
  1. Wouter says:

    Hello Paul, this solutions would be great for us. We are using crm online 2015. Do you have any recommandations how to implement it for a online scenarion

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s