AnsweredAssumed Answered

Filter by ID

Question asked by Francesca Shiekh on Jun 15, 2015
Latest reply on Feb 11, 2016 by varun_gupta34
I have a custom filter that I would expect to be able to use with an array of ids to open a selection-list drawer with only the accounts in the array.

<?php
  $viewdefs['Accounts']['base']['filter']['basic']['filters'][] = array(
  'id' => 'FilterAccountsIdTemplate',
  'name' => 'LBL_FILTER_ACCOUNTS_BY_ID_TEMPLATE',
  'filter_definition' => array(
    array(
      'id' => array(
        '$in'=>'',
      ),
    ),
  ),
  'editable' => true,
  'is_template' => true,
);
?>

The ids are returned by an API call which gets the Account IDs related to a Contact (we have am M-N relationship between Accounts and Contacts).
The intent is, if there is only one account related to the contact, populate the Account relate field on this module, else, present the user with a selection-list drawer with only Accounts related to the selected Contact

({  extendsFrom: 'CreateActionsView',
  initialize: function(options){
    this._super('initialize', [options]);
    this.model.on('change:case_primary_contact_c', this.updateAccount, this);
  },
  updateAccount: function() {   
    console.log('updateAccount');
    if(!_.isEmpty(this.model.get('contact_id_c'))){
      var contact_id = this.model.get('contact_id_c'),
          url = app.api.buildURL('Contacts/'+contact_id+'/link/accounts_contacts/');
      app.api.call('GET', url, null, {
        success: _.bind( function (data){
          console.log(data);
          if(data.records.length == 1){
            this.model.set('account_id', data.records[0].id);
            this.model.set('account_name', data.records[0].name);
          }else if(data.records.length >1){
           console.log('more than 1');
            var account_ids = [];
            _.each(data.records, function (record){
              account_ids.push(record.id); //generate array of Account ids
            });
            console.log(account_ids);
            var filterOptions = new app.utils.FilterOptions()
              .config({
                'initial_filter': 'FilterAccountsByIdTemplate',
                'initial_filter_label': 'LBL_FILTER_ACCOUNTS_BY_ID_TEMPLATE',
                'filter_populate': {
                 'id':account_ids,
                }
              }).format();
            console.log(filterOptions);
            app.drawer.open({
              layout: 'selection-list',
              context: {
                module: 'Accounts',
                filterOptions: filterOptions,
              }
            });
          }
        }, this),
        error:_.bind(function(o){
          console.log("Error retrieving Account related to Contact" + o);
        }, this),
      });
    }
  },

But, although the filter shows in the selection-list, the criteria is not applied (it shows all accounts as if unfiltered)

I suspect the problem is filtering by ID, which is not normally a filter field.

Any suggestions on how to get around this?
FrancescaS

Outcomes