I faced a scenario where the client want to filter subgrid lookup view dynamically
We have two custom entities with the name Case filling and Application Party, both entities have a lookup of Case entity.
Case Filling entitiy has one to many relationship with the Application Party entity.
On Case filling entity subgrid of Application added.
the client wants to show only those records in subgrid add existing lookup view which is related to the case entity which is set on case filling.
To achieve the above goal I customized the command of Add existing button of Appliation entity.
and bind a js webresource.
When clicking on add existing button a lookup view dialog is opened, the client want to show only those record which is related to the case entity.
I used the below Js code and filter records dynamically by taking help from this link.
function filterApplicationLookupView(formContext) {
debugger;
var caseid = null;
var searchText = "";
//var formContext = executionContext.getFormContext();
if (formContext.getAttribute("ms_case") != null && formContext.getAttribute("ms_case").getValue() != null)
caseid = formContext.getAttribute("ms_case").getValue()[0].id;
caseid = caseid.replace("{", "").replace("}", "");
if (caseid != null) {
var lookupOptions = {
defaultEntityType: "ms_applicationparty",
entityTypes: ["ms_applicationparty"],
defaultViewId: "B538E004-8EE7-4B33-B51F-2F97A62C940A",
viewIds: ["B538E004-8EE7-4B33-B51F-2F97A62C940A"],
allowMultiSelect: true,
disableMru: true,
filters: [{ filterXml: "<filter type='and'><condition attribute='ms_case' operator='eq' value='" + caseid + "' /><condition attribute='ms_casefiling' operator='null' /></filter>", entityLogicalName: "ms_applicationparty" }],
searchText: ''
};
Xrm.Utility.lookupObjects(lookupOptions)
.then(
function (result) {
if (result == null || result.length == 0) {
} else {
var appParties = [];
for (var i = 0; i < result.length; i++) {
appParties.push(result[i].id)
}
for (var i = 0; i < appParties.length; i++) {
var appPartyId = appParties[i];
appPartyId = appPartyId.replace("{", "").replace("}", "");
UpdateCaseFillingOnApplication(appPartyId);
}
}
});
}
}
function UpdateCaseFillingOnApplication(appPartyId) {
debugger;
var serverURL = Xrm.Page.context.getClientUrl();
var fillingId = Xrm.Page.data.entity.getId();
fillingId = fillingId.replace("{", "").replace("}", "");
var filling = {};
//Lookup
filling["ms_CaseFiling@odata.bind"] = "/ms_casefilings(" + fillingId + ")"; //setting existing lookup
var req = new XMLHttpRequest();
req.open("PATCH", serverURL + "/api/data/v9.0/ms_applicationparties(" + appPartyId + ")", false);
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.send(JSON.stringify(filling));
if (req.readyState == 4 /* complete */) {
if (req.status == 204) {
} else {
}
}
}