Thursday, February 28, 2013

Rollup 12 :- Pricelist Item error on Opportunity screen

In the latest roll-up 12 updates, I found an error message displaying on the opportunity screen for the missing price list. If your business not making use of the price list item an OOTB feature, one of the solution you can make use of to suppress the error message, is  by updating a price List Item using a Retrieve plugin. Keep in mind this needs to be configured as a Pre-event stage in the Plugin configuration tool. 

Code sample:

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

namespace Opportunity
{
    public class SetDefaultPriceList : IPlugin
    {
        public void Execute(IServiceProvider sp)
        {
            //--Trigger: Retrieve Message of opportunity
            //--Stage: Pre
            //--Method: Synchronous
            //--Filter Attributes: none
            //--Pre Attributes: none
            //--Post Attributes: none
            //--Description: Setting the the default PriceList value.
            //--Author: Chaitanya

            var tracer = (ITracingService)sp.GetService(typeof(ITracingService));
            var context = (IPluginExecutionContext)sp.GetService(typeof(IPluginExecutionContext));

            if (context.MessageName == "Retrieve" && context.PrimaryEntityName == "opportunity")
            {
                var factory = (IOrganizationServiceFactory)sp.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(null);

                Guid priceListId = Guid.Empty;

                var priceListsEntity = GetDefaultPriceLevel(service);
                if(priceListsEntity.Entities.Count > 0)
                    if(priceListsEntity.Entities[0].Attributes.Count > 0)
                        if(priceListsEntity.Entities[0].Attributes.Contains("pricelevelid"))
                            priceListId = (Guid)priceListsEntity.Entities[0].Attributes["pricelevelid"];
                   if(priceListId != Guid.Empty)
                   {
                       var opportunity = new Entity(context.PrimaryEntityName) { Id = context.PrimaryEntityId };
                       opportunity.Attributes.Add("pricelevelid", new EntityReference("pricelevel",priceListId));
                       service.Update(opportunity);
                   }
            }
        }

        private static EntityCollection GetDefaultPriceLevel(IOrganizationService service)
        {
            var cols = new ColumnSet("pricelevelid", "name");
            var filter = new FilterExpression { FilterOperator = LogicalOperator.And };
            filter.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, new object[] { "Default Price List" }));

            var query = new QueryExpression
            {
                ColumnSet = cols,
                Criteria = filter,
                EntityName = "pricelevel"
            };
            return service.RetrieveMultiple(query);
        }
    }
}
One more thing to keep in mind, Before running this code, you should have a "Default Price List" created with the same name. 

Hope this helps, 
Chaitanya...

Saturday, February 23, 2013

XRM Utility

Some quick tips on the javascript utilities:

for any reason, if the dashboard is clumsy in the real esate in displaying grid and charts, we can provide a button to view in a new tab by exploding or opening the web resource in a new tab.

Code sample:


//HTML Web Resource
Xrm.Utility.openWebResource(
    webResourceName
  ,
    webResourceData
  ,
    width
  , 
    height
  )

//Example 
//Open an HTML web resource named “new_webResource.htm”
Xrm.Utility.openWebResource("new_webResource.htm");

//Open an HTML web resource, setting the height and width
Xrm.Utility.openWebResource("new_webResource.htm", null, 500,700);

//Open an HTML web resource with the parameters expected by HTML web resources
Xrm.Utility.openWebResource("new_webResource.htm?typename=account&userlcid=1033");

//Entity Form:
Xrm.Utility.openEntityForm(
    name
  ,
    id
  ,
    parameters
  )
  
//Example
//Open a new account record
Xrm.Utility.openEntityForm("account");

//Open an existing account record
Xrm.Utility.openEntityForm("account","B85N0252-GHT8-U111-997C-99055D8A8410");

Hope this helps,
Chaitanya...