Skip to content

Instantly share code, notes, and snippets.

@AshV
Last active December 10, 2018 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshV/90aa2dc8f2733384306b5f5dcfbbf95b to your computer and use it in GitHub Desktop.
Save AshV/90aa2dc8f2733384306b5f5dcfbbf95b to your computer and use it in GitHub Desktop.
Set Lookup View dynamically with JavaScript [ Dynamics CRM / 365 ]
// https://www.ashishvishwakarma.com/set-lookup-view-with-javascript-dynamics-crm-365/
function formOnLoad() {
setLookupViewByName("parentaccountid", "Customers", true);
}
function setLookupViewByName(fieldName, viewName, asynchronous) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/savedqueries?$select=savedqueryid&$filter=name eq '" + viewName + "'", asynchronous);
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=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
if (results.value.length > 0) {
var savedqueryid = results.value[0]["savedqueryid"];
Xrm.Page.getControl(fieldName).setDefaultView(savedqueryid);
} else {
Xrm.Utility.alertDialog(viewName + " view is not available.");
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
@AshV
Copy link
Author

AshV commented Sep 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment