Cannot read property of null. Cannot read property ‘account’ of null
After many try many ways to fix “Cannot read property of null” when using Xrm.WebApi from a Webresource, I found the fix to run fine. Because CRM relies on two arrays window.ENTITY_SET_NAMES OR window.top.ENTITY_SET_NAMES to get the entity set name from the logical name and window.ENTITY_PRIMARY_KEYS or window.top.ENTITY_PRIMARY_KEYS to get the primary key property name of the entity.
However, when the WebResource is opening as a pop-up, both the arrays are coming as null and hence the error.
So before calling Xrm.WebApi.execute please just wrote the following lines.
var entityNames = {};
entityNames["account"] = "accounts";
window.ENTITY_SET_NAMES = JSON.stringify(entityNames);
var primaryKeys = {};
primaryKeys["account"] = "accountid";
window.ENTITY_PRIMARY_KEYS = JSON.stringify(primaryKeys).
Code language: JavaScript (javascript)
Hope this will help you to save your time to try many others ways.
But, this is unsupported and maybe it will be worked on future updates.
Thanks