Search code examples
c#dynamics-crm

How to reproduce or fix the string truncation exception in DCRM when inserting a record in an entity?


When creating a record in an entity in DCRM online instance using IOrganizationservice.create() webservice method in c# code, we're getting the following exception in production envirnoment,though the data length in fields are within the DCRM schema allowed limit..

System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: String or binary data would be truncated in table '{0}', column '{1}. Truncated value: {2} ErrorCode: 0x80090429.

If data in fields is longer than the allowed field length of DCRM schema,I get exception as StringLengthTooLong but not the truncation exception.Can anyone help to reproduce the string truncation exception in DCRM?

Following code is used to create a record in entity::

public String StoreUnpublishedData(String entityName,String messageName,String data,String listener,String failedInfo)

   {

       try

       {

           String failedInfo2 = String.IsNullOrWhiteSpace(failedInfo) ? "" : failedInfo;

           if (failedInfo2.Length > 4000)//in case it is too long

               failedInfo2 = failedInfo2.Substring(0, 4000);

           Entity LogEntity = new Entity();

           LogEntity.LogicalName = "unpublisheddata";

           LogEntity.Attributes.Add("id",Guid.NewGuid().ToString().Replace("-", ""));

           LogEntity.Attributes.Add("messageName",messageName);

           LogEntity.Attributes.Add("entityname",entityName);

           LogEntity.Attributes.Add("Listener", listener);

           LogEntity.Attributes.Add("Retries", 0);

           LogEntity.Attributes.Add("failedInfo",failedInfo2);

           LogEntity.Attributes.Add("data", data);

           m_ctx.OrganizationService.Create(LogEntity);

           return newId;

       }

       catch (Exception)

       {

           throw;

       }

   }

line m_ctx.OrganizationService.Create(LogEntity) is throwing exception.

Fields length in DCRM:

Column Name Max Length entityname 100 messagename 100 id 100 failed_info 4000 data 102400 listener 10240


Solution

  • Two quick thoughts on this...

    1. By process of elimination you should be able to identify which attribute is causing the issue. Try commenting out each attribute and see when the error goes away.

    2. Perhaps there is some kind of Unicode encoding issue? As a test, maybe try truncating failedInfo2 to a much-shorter length, like 1500.