Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : faRANymFCSsocuDSEGSqMQ

“Cannot read property ‘<entity name>’ of null”–Error while executing a bound action from from a WebResource in Dynamics V9.0 using Xrm.WebApi.execute

Debajit Dutta Profile Picture Debajit Dutta 2,702

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.StringParam = “Web Api Test”;
     actionRequest.DecimalParam = 30.43;
     actionRequest.entity = { entityType: “account”, id: accountId };


    actionRequest.getMetadata = function () {
         return {
             boundParameter: “entity”,
             operationName: “new_EntityAction”,
             operationType: 0,
             parameterTypes: {
                 “StringParam”: {
                     structuralProperty: 1,
                     typeName: “Edm.String”
                 },


                “DecimalParam”: {
                     structuralProperty: 1,
                     typeName: “Edm.Decimal”
                 },


                “entity”: {
                     structuralProperty: 5,
                     typeName: “mscrm.account”
                 }
             }
         }
     };


    Xrm.WebApi.execute(actionRequest).
         then(function (data) {
             // parsing your results here.


        },
         function (error) {
             debugger;
             console.log(error.message);
         });

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

image

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 = { height: 600, width: 800 };


    var parentAccount = Xrm.Page.getAttribute(“new_parentaccount”).getValue();


    var parentAccountId = ”;
     if (parentAccount != null) {
         parentAccountId = parentAccount[0].id;
     }


    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 may soon work with Microsoft.

However hope it saves some time or now you are aware before hand it will not work for this scenario

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com





This was originally posted here.

Comments

*This post is locked for comments