Description:
In this
example we explain that how to retrieve and execute system view using web API
in Dynamic CRM.or JavaScript code to retrieve and execute view(Predefined
Query) using web API in Dynamic CRM.or how to read or retrieve data from System
view and user View using JavaScript or FetchXML in Dynamic CRM.
Suppose
you would like to retrieve List of “Active Accounts” from your Dynamic CRM
there is already a predefined queries for each view (i.e., System View and User
View queries).
So below
is the code that will help you to retrieve the list of Active Accounts in your
Dynamic CRM using JavaScript and Web API in dynamic CRM.
Below
two function are used to retrieve the data from your view simply by calling
like that
Pass the ‘System View’ name to executeView
({View Name}) function.
View name means like “Active Accounts” or “Active Contacts” anything that you
want to retrieve data for any entities from your Dynamic CRM.
Function :
function executeView(viewName) {
try {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
// Get ‘Active Accounts’ GUID
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/savedqueries?$select=name,savedqueryid&$filter=name eq ‘“ + viewName + “‘“), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
var dat = data.value;
for (var i = 0; i < dat.length; i++) {
var viewId = dat[i].savedqueryid;
alert(“View Id : “ + viewId);
if (viewId) {
executeViewQuery(viewId);
}
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error while fetching view id” + error.message);
}
}
try {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
// Get ‘Active Accounts’ GUID
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/savedqueries?$select=name,savedqueryid&$filter=name eq ‘“ + viewName + “‘“), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
var dat = data.value;
for (var i = 0; i < dat.length; i++) {
var viewId = dat[i].savedqueryid;
alert(“View Id : “ + viewId);
if (viewId) {
executeViewQuery(viewId);
}
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error while fetching view id” + error.message);
}
}
};
req.send();
} catch (e) {
alert(“Error in getViewId” + e.description);
}
}
req.send();
} catch (e) {
alert(“Error in getViewId” + e.description);
}
}
function executeViewQuery(viewId) {
try {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/accounts?savedQuery=“ + viewId + ““), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
// Include this to get Lookup and Optionset text
req.setRequestHeader(“Prefer”, “odata.include-annotations=OData.Community.Display.V1.FormattedValue”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
var dat = data.value;
for (var i = 0; i < dat.length; i++) {
alert(“Account Name – “ + dat[i].name);
// Read Lookup name (i.e.,Primary Contact)
alert(“Primary Contact – “ + dat[i][‘_primarycontactid_value@OData.Community.Display.V1.FormattedValue’]);
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error while executing view query” + error.message);
}
}
};
req.send();
} catch (e) {
alert(“Error in getViewId” + e.description);
}
}
try {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/accounts?savedQuery=“ + viewId + ““), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
// Include this to get Lookup and Optionset text
req.setRequestHeader(“Prefer”, “odata.include-annotations=OData.Community.Display.V1.FormattedValue”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
var dat = data.value;
for (var i = 0; i < dat.length; i++) {
alert(“Account Name – “ + dat[i].name);
// Read Lookup name (i.e.,Primary Contact)
alert(“Primary Contact – “ + dat[i][‘_primarycontactid_value@OData.Community.Display.V1.FormattedValue’]);
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error while executing view query” + error.message);
}
}
};
req.send();
} catch (e) {
alert(“Error in getViewId” + e.description);
}
}
How to Call Function:
// To Retrieve and Execute ‘Active Accounts’
view
executeView(“Active Accounts”);
0 comments:
Post a Comment