web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Problem updating salesorderdetail (Order Product) records using CRM 2011 SDK - "The value of 'salesorderstatecode' on record of type 'salesorderdetail' is outside the valid range."

(0) ShareShare
ReportReport
Posted on by

I am getting and exception while trying to update an existing record in salesorderdetail entity. Below is the exception I am getting

"A validation error occurred.  The value of 'salesorderstatecode' on record of type 'salesorderdetail' is outside the valid range."

The same code is working fine without any issues in our development and test environments. We are having the problem only in our production environment. We compared the meta data of salesorder and salesorderdetail entities and their attributes between test and production environments and could not find any differences.

 

 Below is complete SOAP Fault message

 

<s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode><faultstring xml:lang="en-US">An error occured while processing this request.</faultstring><detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>A validation error occurred.  The value of 'salesorderstatecode' on record of type 'salesorderdetail' is outside the valid range.</Message><StackTrace>&#xD; Server stack trace: &#xD;    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc&amp; rpc)&#xD;    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)&#xD;    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)&#xD;    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)&#xD; &#xD; Exception rethrown at [0]: &#xD;    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)&#xD;    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)&#xD;    at Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest request)&#xD;    at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest request)&#xD;    at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request)&#xD;    at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results)</StackTrace><Type>System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]</Type></InnerException><Message>An error occured while processing this request.</Message><StackTrace>   at CrmPurchaseOrderProxy.UpdatePurchaseOrder(PurchaseOrder upsertPurchaseOrder) in C:\Users\CRM.ProxyService\CRM.ProxyService\CrmPurchaseOrderProxy.cs:line 342&#xD;    at SyncInvokeUpdatePurchaseOrder(Object , Object[] , Object[] )&#xD;    at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)&#xD;    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;    at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>Microsoft.Xrm.Sdk.SaveChangesException</Type></ExceptionDetail></detail></s:Fault>

 

I really appreciate communities help in resolving this critical production issue.

*This post is locked for comments

I have the same question (0)
  • Muhammad Adeel Javaid Profile Picture
    5,580 on at

    I am copying a link below that might be of help for you:

    msdn.microsoft.com/.../microsoft.xrm.sdk.messages.updaterequest.aspx

    http://technet.microsoft.com/en-us/subscriptions/ms941082.aspx

  • JBirnbau Profile Picture
    Microsoft Employee on at

    Hi Rapusu,

    My recommendation would be to query the SalesOrderDetailBase table within your ORG_MSCRM database and check the "salesorderstatecode" column for any NULL or inconsistent values. If all of the values look correct and this is a critical issue for you, I would encourage you to open a support case to investigate further.

  • rapusu Profile Picture
    on at

    Thanks for the reply Muhammad Adeel. The links which you provided did not help. However the problem is fixed. I am going share my finding shortly.

  • rapusu Profile Picture
    on at

    Hi Jon,

    Thanks for the reply. I already checked the salesorderdetailbase table and the salesorderstatecode has value of '0'. I even intercepted the update xml message going to CRM and even it has a value of ‘0’ for salesorderstatecode.

    The problem is now fixed and I am going to share my finding shortly.

    Thanks,

    Suresh

  • Verified answer
    rapusu Profile Picture
    on at

     As I mentioned in the original posting, we are having problem update the salesorderdetail record using CRM SDK only in production environment. We were not having any problem creating a new entry in salesorder and salesorderdetail entities. The exception we were getting while update an existing salesorderdetail record was

     

    “A validation error occurred. The value of 'salesorderstatecode' on record of type 'salesorderdetail' is outside the valid range”

     

    As per the error message, it is clear that the error has occurred while validating the value of “salesorderstatecode”. “salesorderstatecode” attribute in salesorderdetail entity is a read only attribute and we are not setting any value to this attribute. When we checked this attribute metadata, out of the box, no picklist values were configured and the default value is set to “-1”. Moreover we cannot customize this attribute and the question of maybe we did not import the pick list values properly into production environment was quickly ruled out.

     

    As the error message was saying that salesorderstatecode is outside of valid range, we were very much narrow focused and trying to find out if some other custom pick list on salesorderdetails is causing this problem. We compared each and every picklist and their values numerous times and could not find any discrepancy.

     

    After almost 4 days of investigation and comparison of production environment with other working environments, finally yesterday I decided to use reflector to disassemble Microsoft CRM sdk assemblies to find out from where the exception is getting raised and why the validation is failing on salesorderstatecode which is a read only attribute.

     

    Within few minutes I was able to identify the method which is responsible for validating picklist values and the line which is throwing the exception we are getting. Below is the method responsible for picklist validation and the line highlighted in red color box is the line which is throwing an exception and it is matching with the exception we are getting.

     

     

     Source Code (In case if the above image is not clear)

    internal sealed class   PicklistAttributeValidator : AttributeValidatorBase

    {   // Methods

        public override void Validate(Entity   entity, KeyValuePair<string, object> property, AttributeMetadata   attributeMetadata, ExecutionContext platformContext)

        {

            if   (!AttributePluginFilter.IsFilteredOnDisplayMask(attributeMetadata) &&   (entity[property.Key] != null))

            {

                OptionSetValue value2 =   entity[property.Key] as OptionSetValue;

                if (value2 == null)

                {

                    throw new   CrmArgumentException(string.Format(CultureInfo.InvariantCulture,   "Incorrect attribute value type {0}", new object[] {   entity[property.Key].GetType() }), attributeMetadata.Name);

                }

                EnumAttributeMetadata metadata =   attributeMetadata as EnumAttributeMetadata;

                CrmException.Assert(metadata !=   null, string.Format(CultureInfo.InvariantCulture, "Incorrect attribute   metadata type found for {0} - {1}", new object[] { attributeMetadata.Name,   attributeMetadata.GetType() }));

                if   (!metadata.Options.ContainsKey(value2.Value) && ((value2.Value != -1)   || !metadata.IsNullable))   <-- Condition 1

                {

                    if   (((platformContext.CallerOriginToken == null) || !(platformContext.CallerOriginToken.get_CallerOrigin()   is OfflineOrigin)) || !metadata.IsCustomField) < -- Condition 1.1

                    {

                        throw new   CrmPicklistAttributeValidationException(string.Format(CultureInfo.InvariantCulture,   "A validation error occurred.  The   value of '{0}' on record of type '{1}' is outside the valid range.", new   object[] { attributeMetadata.Name, entity.LogicalName }));

                    }

                      entity.Attributes.Remove(property.Key);

                }

            }

        }

    }

                 

    Now carefully looking at the “if condition” which is responsible for throwing this exception (Condition 1.1), the condition is checking if “CallerOriginToken”  is null or if picklist attribute is not a custom field (NOTE THAT IT IS NOT AT ALL CHECKING THE ATTRIBUTES VALUE RANGE) and throwing an exception. After looking at the condition it was clear to me that the exception we were getting was a false and misleading exception.

     

    I know that CallerOriginToken basically inherits SoapHeader (http://msdn.microsoft.com/en-us/library/cc151236.aspx) and it will be null only if the authentication token is not getting set in SoapHeader. Immediately we checked the custom WCF service which is calling CRM SDK and it was reading the credential information from web.config file instead of using the apppool identity. After few modification to the web.config file, the service started using the apppool identity and  we were able to perform update on salesorderdetail entity. Even though the issue was fixed something was bother me. Even though the  custom WCF service was not using the apppool identity, we never had any problem creating new entries in salesorderdeatil entity, we had problem only update. Which to me did not made any sense, if the security token in Soap header is important, we should not be able to create new entries also. So I went back to the original Microsoft code, look at the line marked as “Condition 1”, it is checking if the picklist value is available in the metadata or not and the value is not -1. As I mentioned above, out of the box default value of salesorderstatecode is -1 and hence the if condition will evaluate to false for create request and subsequent nested if condition will not be executed, which means a check for “CallerOriginToken” is not getting executed for create requests. In case of update, we retrieve the salesorderdetail record from database first and then issue an update. When the record is retrieved, salesorderstatecode value is getting returned as “0”. So the if condition in line marked “Condition 1” is evaluating to true and forcing the nested if condition marked as “Condition 1.1” to get executed. As the CallerOriginToken was null case we were getting a misleading exception.  I think the exception should clearly mentioned that the security token is missing instead of saying the value is outside the valid range.

    If Microsoft team is viewing my finding, I really appreciate your comments on my findings.

    -Suresh 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans