document.onreadystatechange = function () {
if (document.readyState == "complete") {
var params = getDataParameters();
openFormWithAlternativeKey(params["altkey"]);
}
}
You take this example and expand it so the entity name and form name could also be passed as parameters making it much more generic and flexible. more code below
function openFormWithAlternativeKey(altKey) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?$select=accountid&$filter=new_externalreference eq '" + altKey + "'", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations="OData.Community.Display.V1.FormattedValue"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var accountid = results.value[i]["accountid"];
Xrm.Utility.openEntityForm("account", accountid);
}
}
}
};
req.send();
}
function getDataParameters() {
var dict = new Object();
var vals = new Array();
if (location.search != "") {
vals = location.search.substr(1).split("&");
for (var i in vals) {
vals[i] = vals[i].replace(/+/g, " ").split("=");
}
for (var i in vals) {
if (vals[i][0].toLowerCase() == "data") {
vals = decodeURIComponent(vals[i][1]).split("&");
for (var i in vals) {
vals[i] = vals[i].replace(/+/g, " ").split("=");
dict[vals[i][0]] = vals[i][1];
}
break;
}
}
}
return dict;
}