Monday, February 07, 2011

Error When a Picklist Option Deleted but with Data Referenced

Today I ran into a strange error when I was trying to open a CRM view. I got a screen shown as below (In case you are curious, I have CRM DevErrors option turned On in my web.config file in order to show CRM error in the following fashion):

image

Basically CRM was complaining with the following message: 

Microsoft CRM Error Report: 
Error Description: 
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Error Details: 
Exception of type 'System.Web.HttpUnhandledException' was thrown.

Full Stack: 
[KeyNotFoundException: The given key was not present in the dictionary.]
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.Crm.BusinessEntities.PagingHelper.GetValueAsString(AttributeInfo attribute, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.PagingHelper.Serialize(OrderExpressionCollection orderCollection, BusinessEntityCollection entities, Int32 pageNum, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.PagingHelper.CreatePagingCookie(BusinessEntityCollection entities, EntityExpression entityExpression, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.QueryProcessObject.AppendPagingCookie(XmlTextWriter xmlWriter, BusinessEntityCollection entities, EntityExpression entityExpression, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.QueryProcessObject.CreateResultSetXmlFromResultEntities(List`1 resultEntities, EntityExpression entityExpression, Boolean moreRecords, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.QueryProcessObject.RetrieveDataQueryAsResultXml(EntityExpression entityExpression, QueryStrategyType strategyType, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.QueryProcessObject.RetrieveQueryAsResultXml(EntityExpression entityExpression, QueryStrategyType strategyType, ExecutionContext context)
at Microsoft.Crm.ObjectModel.SavedQueryService.Execute(String fetchXml, ExecutionContext context)

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.Services.Protocols.LogicalMethodInfo.Invoke(Object target, Object[] values)
at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IPluginExecutionContext context)
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.Execute(String messageName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, PropertyBag fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.RequestBase.Process(Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.RequestBase.Process(CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.CrmServiceInternal.Execute(RequestBase request, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.InProcessCrmService.Execute(Object request)
at Microsoft.Crm.Application.Platform.ServiceCommands.PlatformCommand.ExecuteInternal()
at Microsoft.Crm.Application.Platform.ServiceCommands.ExecuteFetchCommand.Execute()
at Microsoft.Crm.ApplicationQuery.GetViewData()
at Microsoft.Crm.Application.Controls.AppGridDataProvider.GetDataXml(QueryBuilder qb)
at Microsoft.Crm.Application.Controls.AppGridDataProvider.Execute()
at Microsoft.Crm.Application.Controls.AppGridUIProvider.Render(HtmlTextWriter output)
at Microsoft.Crm.Application.Components.UI.DataGrid.RenderData(HtmlTextWriter output)
at Microsoft.Crm.Application.Components.UI.DataGrid.RenderInnerHtml(HtmlTextWriter output)
at Microsoft.Crm.Application.Components.UI.DataGrid.Render(HtmlTextWriter output)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at Microsoft.Crm.Application.Components.UI.CrmUIControlBase.RenderControl(HtmlTextWriter writer)
at ASP.cmis__root_homepage_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

[HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown.]
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.cmis__root_homepage_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

The problem was almost impossible to identify. Even I tried to turn on trace log, I cannot see any helpful message. But fortunately I quickly realized that our business analyst folks have recently deleted an option from a picklist field of this entity. However, there are some data in the system that are still referencing that option. As soon as I realized the cause, it wasn't too hard to fix this issue. What I did was, add back the option that we previously deleted using the same integer option value, publish the entity customization change, and then delete all data referencing that picklist option, and then I can safely delete the same picklist option.

There could be a hundred of different scenarios that could lead to "The given key was not present in the dictionary", the key to determine whether the solution applies to you, is to check that you have "Microsoft.Crm.BusinessEntities.PagingHelper.GetValueAsString(AttributeInfo attribute, ExecutionContext context)" message in the error log, and the error happens when you click to open a CRM view.

I was hoping that CRM platform could have handled this more gracefully, but not a big deal if you know the solution.

Hope this helps if you ever come across the same issue.

Cheers!

No comments:

Post a Comment