Filtering Multiple Models Using a Single Set of Filters (Global Filters)

We are doing “super filtering” in a ton of places on our pages.  Not only for applying a single value across multiple objects, but also same objects in different models - especially aggregate models when we are displaying charts.

We use UI fields on a “dummy” model and then run a snippet when the user clicks a “search” button.

So, create a model to hold your Search UI fields.  We have a Look Ups object that we use everywhere for this, and the user has permission to create records in this object (although they never actually save anything to the object.)

- Add UI fields to your model that are what the user needs to search by (text, picklists, etc.)

 - For your models that you want to affect, create conditions on each model (filter default off).

For us, we may use a field called “Market” to filter our Contact object, Account object, and Opportunity object.  User wants to select market at the top, and the filter would get applied to all three models on the page.

Search Model…
UI field called “Market”.  Picklist values = Energy, Oil & Gas, Education.

Contact Model…
Condition Market__c = {blank value}, off by default, named MarketContact

Account Model…
Condition Market__c = {blank value}, off by default, named MarketAccount

Opportunity Model…
Condition Market__c = {blank value}, off by default, named MarketOpportunity


The snippet looks something like this:

var params = arguments[0],           
 $ = skuid.$;

// Declare your search model                       
var FilterModel = skuid.model.getModel(‘SearchModel’);
var FilterRow = FilterModel.getFirstRow();
           
//**************Include all your Search Fields
var marketVar = FilterModel.getFieldValue(FilterRow,‘Market’,true);
if (marketVar === “”) {marketVar = null;}


// Declare your models you want to affect the conditions on
var contactModel = skuid.model.getModel(‘Contacts’);
contactModel.emptyData();

var AccountModel = skuid.model.getModel(‘Accounts’);
AccountModel.emptyData();

var OppModel = skuid.model.getModel(‘Opportunities’);
OppModel.emptyData();

//***********Declare all Model Conditions 
var contactMarketCondition = contactModel.getConditionByName(‘MarketContact’);
var accountMarketCondition = AccountModel.getConditionByName(‘MarketAccount’);
var oppMarketCondition = OppModel.getConditionByName(‘MarketOpportunity’);


//***********Deactivate all Model Conditions 
contactModel.deactivateCondition(contactMarketCondition, false);
AccountModel.deactivateCondition(accountMarketCondition, false);
OppModel.deactivateCondition(oppMarketCondition, false);


 
//***********Set Model Conditions if search fields are populated
if(marketVar !== null) {contactModel.setCondition(contactMarketCondition, marketVar, false);}
if(marketVar !== null) {AccountModel.setCondition(accountMarketCondition, marketVar, false);}
if(marketVar !== null) {OppModel.setCondition(oppMarketCondition, marketVar, false);}



// query models with conditions set
skuid.model.updateData([contactModel],function(){
    console.log(‘Contact model updated!’);
});

skuid.model.updateData([AccountModel],function(){
    console.log(‘Account model updated!’);
});

skuid.model.updateData([OppModel],function(){
    console.log(‘Opp model updated!’);
});