Last active
December 10, 2018 19:10
-
-
Save AshV/90aa2dc8f2733384306b5f5dcfbbf95b to your computer and use it in GitHub Desktop.
Set Lookup View dynamically with JavaScript [ Dynamics CRM / 365 ]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find companion article here https://www.ashishvishwakarma.com/set-lookup-view-with-javascript-dynamics-crm-365/