Skip to main content

Filter subgrid using javascript in Dynamics 365

Filter subgrid using javascript in Dynamics 365. 

Below is JS code which we need to call on + new button of subgrid

Add the below JS using ribbon bench + new button of subgrid

Step 1 : Create your function e.g FilterPartnerInPromotion()
Step 2 :  Call the function on click on + button of subgrid. Follow the below image
Register your webresource and function on ribbon command.

Use case : We have to filter partner grid (Custom Entity ) based on certain feild (Distrubution channel) on promotion (Custom entity ) form

Modify the filter and schema based on your condition. Focus on red highlighted lines fetch xml and layout xml you can put based on your use case.
// to filter promotion
function FilterPartnerInPromotion() {
debugger;
try {
var entityName = Xrm.Page.data.entity.getEntityName();
if (entityName == "zh_promotion") {  // to check filter is only on this entity you can remove
var viewId = "{72A70595-C721-4400-8D9B-1612E2A570DE}";   //view id of lookup view of entity to be filtered
var PromotionDistrubutionChannel = Xrm.Page.getAttribute("zh_promotionchannel").getValue();
if (PromotionDistrubutionChannel != null) {
var subgridLookup = Xrm.Page.getControl("lookup_Partner");  // Important it will always be lookup_YourEntityName not schema name 
subgridLookup.addPreSearch(function () {
var fetchxml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='zh_partner'>" +
"<attribute name='zh_name' />" +
"<attribute name='zh_individualcianumber' />" +
"<attribute name='zh_groupcia' />" +
"<attribute name='zh_distributionchannel' />" +
"<attribute name='zh_partnerid' />" +
"<order attribute='zh_name' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='zh_distributionchannel' operator='eq' value='" + PromotionDistrubutionChannel + "' />" +
"</filter>" +
"<link-entity name='zh_partner' from='zh_partnerid' to='zh_groupcia' visible='false' link-type='outer' alias='a_e3e60fcc0147e811a95a000d3a828e95'>" +
"<attribute name='zh_individualcianumber' />" +
"</link-entity>" +
"</entity>" +
"</fetch>";
var layout = "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
"<row name='result' id='zh_partnerid'>" +
"<cell name='zh_name' width='200' />" +
"<cell name='zh_individualcianumber' width='200' />" +
"<cell name='zh_distributionchannel' width='200' />" +
"<cell name='zh_groupcia' width='200' />" +
"</row>" +
"</grid>";
subgridLookup.addCustomView(viewId, "zh_partner", "Partner Lookup View", fetchxml, layout, true);
});
}
}
}
catch (err) {
WriteToErrorLog(err.message, "FilterPartnerInPromotion", "zh_UpdateQuoteFeilds");
}
}

RESULTS: After you click + the search. You can see my subgrid is filtered based on promotion channel. Cheers :-)


Re

Comments

  1. hi can it be possible istead + button click directly in subgrid list it will show related records ?

    ReplyDelete
  2. In case entity contact lookup is there so here based on lookup value related case list should show in subgrid in case entity,We can use quick view form as well but i need to use script side .

    ReplyDelete

Post a Comment