Filter by ID

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
Parents
  • Hi FrancescaS,

    Yes I too have faced this problem.Then I used  'name' instead of id and it started working for me.Also I believe the filter definition should be like this.
    'filter_definition' => array(
        array(
          'name' => array(
            '$in'=> array(),
          ),
        ),
      ),

    You can use $in => array() instead of an empty string as we are going to pass an array of account names.

    Hope it helps.
    Thanks!
  • I found that the problem is not the id but rather the operator, I can pass a single ID and it will work but if I pass an array with an $in operator it gives me an error "Error: Did not recognize the operand: 1".

    The following works:
    <?php
      $viewdefs['Accounts']['base']['filter']['basic']['filters'][] = array(
      'id' => 'FilterAccountsByIdTemplate',
      'name' => 'LBL_FILTER_ACCOUNTS_BY_ID_TEMPLATE',
      'filter_definition' => array(
        array(
          'id' => array(
            '$equals'=>'',
          ),
        ),
      ),
      'editable' => true,
      'is_template' => true,
    );
    ?>

    but 

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

    The filter I  see does not have an option 'is one of' so there is a 'Select' in the slot where the filter should be...
    ...back to the drawing board.

    FrancescaS
Reply
  • I found that the problem is not the id but rather the operator, I can pass a single ID and it will work but if I pass an array with an $in operator it gives me an error "Error: Did not recognize the operand: 1".

    The following works:
    <?php
      $viewdefs['Accounts']['base']['filter']['basic']['filters'][] = array(
      'id' => 'FilterAccountsByIdTemplate',
      'name' => 'LBL_FILTER_ACCOUNTS_BY_ID_TEMPLATE',
      'filter_definition' => array(
        array(
          'id' => array(
            '$equals'=>'',
          ),
        ),
      ),
      'editable' => true,
      'is_template' => true,
    );
    ?>

    but 

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

    The filter I  see does not have an option 'is one of' so there is a 'Select' in the slot where the filter should be...
    ...back to the drawing board.

    FrancescaS
Children
No Data