Skip to content

Instantly share code, notes, and snippets.

@MaceWindu1
Last active October 28, 2019 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaceWindu1/43f6de12837e35f6fcbc7d2168969f68 to your computer and use it in GitHub Desktop.
Save MaceWindu1/43f6de12837e35f6fcbc7d2168969f68 to your computer and use it in GitHub Desktop.
Filter Subgrid
async function filterTagsSubgrid(executionContext: any): Promise<void> {
try {
var formContext: any = executionContext.getFormContext();
}
catch (e) {
var formContext: any = executionContext;
};
var formType: number = formContext.ui.getFormType();
var values: any = [0, 1, 4, 6];
if (values.includes(formType)) return;
// field to filter by
var accountId: string = formContext.getAttribute("parentaccountid").getValue()[0].id.substring(1, 37);
// subgrid you want to filter
var controlSubgrid: any = formContext.getControl("Tags");
// retrieve related records (name of the intersect entity, field to get & field to filter by + the value)
var results: Xrm.RetrieveMultipleResult = await Xrm.WebApi.online.retrieveMultipleRecords("talxis_talxis_tag_account", "?$select=talxis_tagid&$filter=accountid eq " + accountId);
// add results to array
var list: Array<string> = [];
for (var i: int = 0; i < results.entities.length; i++) {
var talxis_tagid: string = results.entities[i]["talxis_tagid"];
list.push(talxis_tagid);
}
// put the results from array to string
var tags: string = "";
for (var i: int = 0; i < list.length; i++) {
tags += '<condition attribute="talxis_tagid" operator="eq" value="' + list[i] + '" />'
};
// show empty view if there are no related records
if (tags == "") {
var fetchXML: string = '<fetch version="1.0" output-format="xml - platform" mapping="logical" distinct="false">' +
'<entity name = "talxis_tag" >' +
'<filter type="and">' +
'<condition attribute="talxis_name" operator="null" />' +
'</filter>' +
'<order attribute="talxis_name" descending="false" />' +
'<attribute name = "talxis_name">' +
'<attribute name = "talxis_tagid">' +
'</entity>' +
'</fetch>';
}
// else show the related records
else {
var fetchXML: string = '<fetch version="1.0" output-format="xml - platform" mapping="logical" distinct="false">' +
'<entity name = "talxis_tag" >' +
'<filter type="and">' +
'<condition attribute="statecode" operator="eq" value="0" />' +
'<filter type="or">' +
tags +
'</filter>' +
'</filter>' +
'<order attribute="talxis_name" descending="false" />' +
'<attribute name = "talxis_name">' +
'<attribute name = "talxis_tagid">' +
'</entity>' +
'</fetch>';
}
// display the new fetch xml
await controlSubgrid.setFilterXml(fetchXML);
// refresh the subgrid
controlSubgrid.refresh();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment