Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
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
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. The same would work if the webresource is embedded within the CRM Form.
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 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
Debajit Dutta
(Dynamics MVP)
For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
Thanks Debajit. You are an inspiration.
Thanks Abhishek for reading my blog
HI Debajit,
I am trying to use parent.Xrm.WebApi.retrieveMultipleRecords to retrieve the custom Entity values in 9.1 D365, i am getting below mentioned error while retrieving the records.
Cannot read property ‘Entityname’ of null
we are calling this from HTML, we are not using ClientGlobalContext.js.aspx, inside the HTML. since its deprecating soon. Please suggest or help me to resolve this issue.
Regards,
Krishna.
Hi Krishna,
May be you can look into this. Seems like the same error – https://debajmecrm.com/2018/09/29/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/
Cheers!
Debajit Dutta
HI Debajit,
Thanks for your reply , tried the same thing but still i am getting the same error. Please suggest any suggestions for the same.
Regards,
Krishna.
HI Debajit,
Below is my attached code and Error details same as well, Please let me know any changes are needed for code.
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
parent.Xrm.WebApi.retrieveMultipleRecords(“account”, “?$select=name”).then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
console.log(result.entities[i]);
}
// perform additional operations on retrieved records
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
Error Message: Cannot read property 'account' of null
Regards,
Krishna.