C# Code · D365 Error · Dynamics Plugins

{Dynamics CRM Error} The formatter threw an exception while trying to deserialize the message

Today while working on one of the queryexpression code in my project I faced this issue.

Error Details

Message=The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:request. The InnerException message was 'Error in line 1 position 1836. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'FaultMetadataToCDSAlert:IotAlertStateCode'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'IotAlertStateCode' and namespace 'FaultMetadataToCDSAlert'.'.  Please see InnerException for more details.
  Source=Microsoft.PowerPlatform.Cds.Client
  StackTrace:
   at Microsoft.PowerPlatform.Cds.Client.CdsServiceClient.RetrieveMultiple(QueryBase query)
   at FaultMetadataToCDSAlert.Program.Main(String[] args) in D:\Microsoft\repo\Samples\Samples\src\Create-Dynamics-Entities\FaultMetadataToCDSAlert\Program.cs:line 89

C# Code

// build query expression that finds the Iot Alert with the Hash value supplied in the fault
                QueryExpression queryExpression = new QueryExpression()
                {
                    Distinct = false,
                    EntityName = DynamicsEntities.IoTAlert,
                    ColumnSet = new ColumnSet(IoTAlertProperties.MsdynAlertToken),
                    Criteria =
                {
                    Filters =
                        {
                            new FilterExpression
                            {
                                Conditions =
                                {
                                    new ConditionExpression(IoTAlertProperties.MsdynAlertToken, ConditionOperator.Equal, alertToken),
                                    new ConditionExpression(IoTAlertProperties.StateCode, ConditionOperator.NotEqual, IotAlertStateCode.Inactive),
                                },
                            },
                        },
                },
                };
                queryExpression.AddOrder(IoTAlertProperties.MsdynAlertTime, OrderType.Descending);

                // run the query to determine if the IoT Alert already exists in Dynamics
                EntityCollection queryExpressionResult = cdsClient.RetrieveMultiple(queryExpression);

In FilterExpression I was taking value from enum for the StateCode field

new ConditionExpression(IoTAlertProperties.StateCode, ConditionOperator.NotEqual, IotAlertStateCode.Inactive),

To Fix this issue we need to add data type and provide valid cast object.

new ConditionExpression(IoTAlertProperties.StateCode, ConditionOperator.NotEqual, (int)IotAlertStateCode.Inactive),

Aletrnatively, When you create a condition that compares an attribute value to an enumeration, such as a state code, you must use the ToString method to convert the value to a string.

new ConditionExpression(IoTAlertProperties.StateCode, ConditionOperator.NotEqual, IotAlertStateCode.Inactive.ToString()),

Hope this helps!

Advertisement

One thought on “{Dynamics CRM Error} The formatter threw an exception while trying to deserialize the message

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s