Skip to content

Instantly share code, notes, and snippets.

@melamriD365
Created April 29, 2019 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save melamriD365/dfb145dfbb641262bd857b64af64ceec to your computer and use it in GitHub Desktop.
Save melamriD365/dfb145dfbb641262bd857b64af64ceec to your computer and use it in GitHub Desktop.
set fetchXml dynamically to a subgrid in classic UI and Unified Client Interface
function FetchViaName(executionContext) {
var formContext = executionContext.getFormContext();
var fullname = null;
var tabObj = formContext.ui.tabs.get("tab_summary");
var secObj = tabObj.sections.get("sec_contactCompaigns");
secObj.setVisible(false)
var isUnified = isUCI()
//--------------------------------------Unified Interface----------------------------------------//
if (isUnified) {
console.log("IsUnified Interface....TRUE");
var firstName = formContext.getAttribute("firstname").getValue();
var lastName = formContext.getAttribute("lastname").getValue();
if (firstName == null || lastName == null) {
setTimeout(function () { FetchViaName(executionContext); }, 2000);
return;
}
fullname = firstName + " " + lastName;
console.log("fetch compaignsContact: " + fullname);
var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">' +
'<entity name="campaign">' +
'<attribute name="name" />' +
'<order attribute="name" descending="true" />' +
'<link-entity name="campaignitem" from="campaignid" to="campaignid" visible="false" intersect="true">' +
'<link-entity name="list" from="listid" to="entityid" alias="al">' +
'<link-entity name="listmember" from="listid" to="listid" visible="false" intersect="true">' +
'<link-entity name="contact" from="contactid" to="entityid" alias="am">' +
'<filter type="and">' +
'<condition attribute="fullname" operator="eq" value="' + fullname + '"' + '/>' +
'</filter>' +
'</link-entity>' +
'</link-entity>' +
'</link-entity>' +
'</link-entity>' +
'</entity>' +
'</fetch>';
var contactCompaigns = formContext.getControl("ContatCompaigns");
if (contactCompaigns == null) {
setTimeout(function () { FetchViaName(executionContext); }, 2000);
return;
}
else {
//set fetchXml
var setFetchXmlStr = Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml.toString();
var newFunc = setFetchXmlStr.replace("function(e){", "function(e){if (e.indexOf('ZZZAAA') >= 0) {e = fetchXml;}");
eval("Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml=" + newFunc);
contactCompaigns.refresh();
secObj.setVisible(true)
console.log("refresh............")
}
}
//--------------------------------------legacy web client----------------------------------------//
else {
console.log("IsUnified Interface....FALSE");
fullname = formContext.getAttribute("fullname").getValue();
console.log("FullName: " + fullname)
var contactCompaigns = window.parent.document.getElementById("ContatCompaigns");
if (contactCompaigns == null) {
setTimeout(function () { FetchViaName(executionContext); }, 2000);
return;
}
else {
var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">' +
'<entity name="campaign">' +
'<attribute name="name" />' +
'<order attribute="name" descending="true" />' +
'<link-entity name="campaignitem" from="campaignid" to="campaignid" visible="false" intersect="true">' +
'<link-entity name="list" from="listid" to="entityid" alias="al">' +
'<link-entity name="listmember" from="listid" to="listid" visible="false" intersect="true">' +
'<link-entity name="contact" from="contactid" to="entityid" alias="am">' +
'<filter type="and">' +
'<condition attribute="fullname" operator="eq" value="' + fullname + '"' + '/>' +
'</filter>' +
'</link-entity>' +
'</link-entity>' +
'</link-entity>' +
'</link-entity>' +
'</entity>' +
'</fetch>';
if (contactCompaigns.control != null) {
//Here i set the fetchxml directly to subgrid
contactCompaigns.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid
contactCompaigns.control.Refresh(); //refresh the sub grid using the new fetch xml
secObj.setVisible(true)
}
else {
setTimeout(function () { FetchViaName(executionContext); }, 2000);
}
}
}
}
function isUCI() {
return Xrm.Internal.isUci()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment