Hi all,
In today's post I would like to discuss the working code to apply custom FetchXml to a subgrid using JavaScript in Dynamics CRM 2015 update 1.
In this code snippet, I am trying to populate a grid in the opportunity form with all the opportunities that have the same account associated with them as the present opportunity once a value is present in the Account field. So I bind this method to the onLoad of the opportunity form and also to the onChange of the account field.
///method to filter the opportunities and show only those opportunities that share the same account
function filterRelatedOpportunitiesSubGrid() {
////window.parent to account for the latest changes in turbo forms in Dynamics CRM
var relatedProjectsSubGrid = window.parent.document.getElementById("RelatedProjects");
if (null != relatedProjectsSubGrid) {
var parentAccountId = "parentaccountid";
var selectedCustomer = controlExists(parentAccountId) ? Xrm.Page.getAttribute(parentAccountId).getValue() : null;
if (selectedCustomer != null) {
var accountId = selectedCustomer[0].id;
if (accountId != null) {
var fetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
"<entity name='opportunity'>" +
"<attribute name='name'/>" +
"<attribute name='opportunityid'/>" +
"<attribute name='parentaccountid'/>" +
"<order descending='false' attribute='name'/>" +
"<filter type='and'>" +
"<condition attribute='parentaccountid' value='" + accountId + "' operator='eq'/>" +
"</filter>" +
"</entity>" +
"</fetch>";
if (relatedProjectsSubGrid.control != null) {
relatedProjectsSubGrid.control.SetParameter("fetchXml", fetchXml);
relatedProjectsSubGrid.control.Refresh();
} else {
////try again if the control property has not loaded completely
setTimeout(function () { filterRelatedOpportunitiesSubGrid(); }, 2000);
}
}
}
} else {
////Try again in case the sub-grid has not loaded yet
setTimeout(function () { filterRelatedOpportunitiesSubGrid(); }, 2000);
}
}
function controlExists(controlName) {
var control = Xrm.Page.getControl(controlName);
var attribute = Xrm.Page.getAttribute(controlName);
return control != null && attribute != null;
}
In today's post I would like to discuss the working code to apply custom FetchXml to a subgrid using JavaScript in Dynamics CRM 2015 update 1.
In this code snippet, I am trying to populate a grid in the opportunity form with all the opportunities that have the same account associated with them as the present opportunity once a value is present in the Account field. So I bind this method to the onLoad of the opportunity form and also to the onChange of the account field.
///method to filter the opportunities and show only those opportunities that share the same account
function filterRelatedOpportunitiesSubGrid() {
////window.parent to account for the latest changes in turbo forms in Dynamics CRM
var relatedProjectsSubGrid = window.parent.document.getElementById("RelatedProjects");
if (null != relatedProjectsSubGrid) {
var parentAccountId = "parentaccountid";
var selectedCustomer = controlExists(parentAccountId) ? Xrm.Page.getAttribute(parentAccountId).getValue() : null;
if (selectedCustomer != null) {
var accountId = selectedCustomer[0].id;
if (accountId != null) {
var fetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
"<entity name='opportunity'>" +
"<attribute name='name'/>" +
"<attribute name='opportunityid'/>" +
"<attribute name='parentaccountid'/>" +
"<order descending='false' attribute='name'/>" +
"<filter type='and'>" +
"<condition attribute='parentaccountid' value='" + accountId + "' operator='eq'/>" +
"</filter>" +
"</entity>" +
"</fetch>";
if (relatedProjectsSubGrid.control != null) {
relatedProjectsSubGrid.control.SetParameter("fetchXml", fetchXml);
relatedProjectsSubGrid.control.Refresh();
} else {
////try again if the control property has not loaded completely
setTimeout(function () { filterRelatedOpportunitiesSubGrid(); }, 2000);
}
}
}
} else {
////Try again in case the sub-grid has not loaded yet
setTimeout(function () { filterRelatedOpportunitiesSubGrid(); }, 2000);
}
}
function controlExists(controlName) {
var control = Xrm.Page.getControl(controlName);
var attribute = Xrm.Page.getAttribute(controlName);
return control != null && attribute != null;
}