Reading data in Dynamics Portal is always a tricky part.
Enabling OData option for the entity creating Entity List is one of the mostly used method to retrieve data from CRM.
You need to enable ODATA on Entity List record as shown below:

And then querying the OData through JavaScript we get the data for Entity.

Everything was working fine as expected till months. Suddenly the client reported that they are not getting the desired results.
Then on investigating the issue I found the below interesting tip which I would like to share with all here
On debugging the issue, I found we were getting the below error:

“500 (Internal Server Error) “. So, wasted more time in trying to find the exact error as debugging the scripts in Portal is troublesome.
So later I just checked the url to check OData Enabled Entities
[portal url without language code]/_odata
In my case: https://blog4.microsoftcrmportals.com/_odata/

Exception: “Could not find an View (savedquery) record where savedqueryid equals 49116ae9-c668-e811-a95d-000d3a1ca939.”
From this I understood that the exact error that was causing the OData request to fail is the view is missing or deleted from system.
So, from the error it was clear that one of my view was used in an Entity List was deleted from the System but as the error is a generic one I couldn’t get the exact Entity for which it was showing the error.
So, I had to later do an Advanced Find with below queries and figure out the issue causing record:

Luckily there were 2 records only one for the Contact and one for the Account.
And for the Account I found the reference was blank as below:

So, I resolved the issue setting the appropriate View in the above setting.
However, I thought the fields should have been mandatory when I enable the OData but they were not.
So, give it a more try I enabled one more Entity List and didn’t select any of the view. And then I again checked if everything is working fine or not.
And guess what everything was working without any issue.
So what was the reason it was failing when the view was deleted for my first scenario?
In Entity List we have a field named “OData View” which stores the view id which it tries to retrieve.
And based on that I found the below 2 observations
Case 1: When the view was deleted
When the view was deleted though it was not shown in the record the reference was not removed from OData View field so it was failing and causing the issue.
Case 2: When just OData was enabled and no view was selected
When only OData is enabled for entity list and no view is selected for it the OData view remains Empty and so no error is thrown in this case.
For reference:

So next time if you face any such issue you can have the OData view compared and resolve the issue 😉
Hope this helps.
Sid 🙂
