0

I have a .NET 4.6.1 web application that queries Dynamics 365 (F&O) custom entities to retrieve some data. When the result set returned by the query has a single quote in it, the code throws Microsoft.OData.ODataexception: Found an unbalanced bracket expression Exception

I am using the OData v4 Client Code Generator version 7.5.1 to generate the metadata. One of my other applications have a similar configuration and it works there without any issues. I do not know what causes this exception in this particular application.

I tried to manually remove the single quote from the result set that I expect and then it works without any errors. It is common to have single quotes in our data and so I need to make it work with it. Not sure if/how I need to escape the single quotes from the resulting data set.

Notice the single quote in the Name field of the data. Manually removing this single quote from D365 UI allows this result to be returned in the code but if I let it as it is, it gives the error.DataSet Image

Any help is appreciated.

EDIT: Here is the code that is throwing the exception

dynamic locations = (from l in d365.HBSWEBServiceLocations
                         where (string.IsNullOrEmpty(location) || l.Name.Equals(location + "*")) &&
                         (string.IsNullOrEmpty(storeNum) || l.StoreNum.Equals(storeNum + "*")) &&
                         (string.IsNullOrEmpty(address) || l.AddressStreet.Equals(address + "*")) &&
                         (string.IsNullOrEmpty(city) || l.AddressCity.Equals(city + "*")) &&
                         (string.IsNullOrEmpty(state) || l.AddressState.Equals(state + "*")) &&
                         (string.IsNullOrEmpty(zip) || l.AddressZipCode.Equals(zip + "*")) &&
                         (string.IsNullOrEmpty(country) || l.AddressCountryRegionId.Equals(country + "*")) &&
                         (l.Status == Els_SMAServiceLocationStatus.New || l.Status == Els_SMAServiceLocationStatus.Active)
                       && l.DataAreaId == "hsvc"
                         orderby l.StoreNum descending, l.Name
                         select new { l.LocationId, l.Name, l.AddressStreet, l.AddressCity, l.AddressState, l.AddressZipCode, l.AddressCountryRegionId, l.SegmentId, l.StoreNum, l.MarketSegment, l.Chain }).Take(20).ToList();
Dipen
  • 1
  • 3
  • One approach could be to remove step by step the differences between the failing and the working application to find out what causes the issue. Are both applications running against the same D365FO environment? Also, could you share a bit of the code around the line where the exception is thrown? Another question, does the query run in e.g. Postman without issue? – FH-Inway May 20 '20 at 19:36
  • @FH-Inway Both the failing and working application run against the same D365FO environment but the entities that they are querying are different (both use custom entities though). Adding the code in the question above. The query that is failing is not returning any results in Fiddler and Postman (the response status is 200 OK but the collection is empty). If I run it directly in the browser, that works – Dipen May 21 '20 at 14:19

1 Answers1

0

This resolved the "Found an unbalanced bracket expression" error for me.

Adding a postLoad method to the data entity can help format the data after the entity is loaded. This will prevent bad characters in the field from causing the Odataexception to be thrown.

You can do this by viewing the code of your ServiceLocations entity. Or open the Data Entity in designer, right click Methods, and select New Method.

Here you can add the following code:

public void postLoad()
{
   super();
   if (strFind(this.Name,"'",1,strLen(this.Name)))
   {
      this.Name = strRem(this.Name,"'");
   }
}
gpemp
  • 11
  • 1