This one drove me crazy and believe these kind of things pop-up the most when you don’t expect them at all. A simple training going on and I was demoing them the wonderful Xrm.WebApi methods. Then came the turn of using Xrm.WebApi.execute to execute a bound action or entity action.
Since I worked a lot recently on Xrm.WebApi methods, was pretty confident of doing and just took this one example out of a participant wish. So here goes the requirement.
- There is a custom entity (let’s call it Test Entity) which has N:1 relation with account. So account is a lookup on the Test Entity form.
- There is a ribbon button on the Test Entity form which when clicked would open up a webresource which would show all the contacts related to the account in the form of HTML table.
So simple isn’t it. In the days when we moved to PowerApps and Flows, this just seems a walk in the park.
So here I started from scratch and wrote the below code onload of the HTML webresource.
var actionRequest = ;
var crmContext = Xrm.Utility.getGlobalContext();
var qString = crmContext.getQueryStringParameters();
var accountId = qString.Data.replace(“}”, “”).replace(“;
actionRequest.getMetadata = function () ,
then(function (data) ,
function (error) );
Before we look at the code above, let’s find out the action.
It’s a bound action for the account entity which has two input parameters – String Parameter & Decimal Parameter
Button is in place and then just as i click the button and HTML webresource pops out boom! An error flashing – Cannot read property ‘account’ of null
For readers who are curious to know how I opened the WebResource, here is the code below.
var webResourceName = “new_/pages/spa.html”;
var windowOptions = ;
var parentAccount = Xrm.Page.getAttribute(“new_parentaccount”).getValue();
var parentAccountId = ”;
if (parentAccount != null)
Xrm.Navigation.openWebResource(webResourceName, windowOptions, parentAccountId);
Such an embarrassment in front of a big audience. Somehow I made this understand and they took it pretty well. If you are thinking that whether I included ClientGlobalContext.js.aspx, yeah I did. And did all whatever it takes to make it work. But none worked.
Searched the heck out of google, no luck as well.
Was not in a mood to leave. I started debugging, went inside all the system files stepping through each lines of the thousands of lines of system code.
And finally the Eureka moment. It was failing at the call of Xrm.Utility.getEntitySetName.
But let’s understand why? Well 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.
Because 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 i just wrote the following lines.
var entNames = ;
entNames[“account”] = “accounts”;
window.ENTITY_SET_NAMES = JSON.stringify(entNames);
var primaryKeys = ;
primaryKeys[“account”] = “accountid”;
window.ENTITY_PRIMARY_KEYS = JSON.stringify(primaryKeys).
And this time when I run, what a relief. The code just ran fine and finally I could see my debugger being hit in the success block.
But please bear in mind this is unsupported and is never suggested. This post is more to highlight why it does not work in this scenario. May it start’s working in a future release.
However hope it saves some time or now you are aware before hand it will not work for this scenario
Published at Sat, 29 Sep 2018 13:57:56 +0000