AnsweredAssumed Answered

Dynamically filter Contacts by Account in Quotes module

Question asked by Richard Coleman on Jan 4, 2019
Latest reply on Jan 17, 2019 by Richard Coleman

Hello,

 

We have a requirement in the Quotes module to filter Contact records in the select2 relate field so that they are filtered by the currently selected Account. In other words, if a billing account is selected in Quotes, we should only see contacts related to that Account. If no billing account is selected, we should see all of them.

 

We have this partially working by way of the initial filters referenced here .

 

The problem we have, is that it works correctly when an Account is selected, but cannot find any contacts when no account is selected.

 

My filter code is here:

$viewdefs["Contacts"]["base"]["filter"]["basic"]["filters"][]=array(
  'id' => 'filterContactsByAccountId',
  'name' => 'LBL_ACCOUNT_ID_CONTACT',
  'filter_definition' => array(
    array(
      'account_id' => ''
    )
  ),
  'editable' => true,
  'is_template' => true,
);

 

The relevant bit of record.php is here (I know that I ideally want to have this in an extension, but for easy of replication, here is the below:

 

//...
2 => array(
    "name"=>"billing_contact_name",
    "initial_filter"=>"filterContactsByAccountId",
    "initial_filter_label"=>"LBL_ACCOUNT_ID_CONTACT",
    "filter_relate"=>array(
        "billing_account_name"=>"account_id"
    )
),
//...

 

And finally, I have put the below into custom/modules/Quotes/clients/base/fields/relate/relate.js . The docs don't say what area to put the JS excerpts in, so I'm not sure if this is the best place to put it, but in lieu of anything else, that made the most sense.

 

({
    extendsFrom: 'RelateField',
    initialize: function (options) {
        this._super('initialize', [options]);
    },
    //Set the filter specifically for a_address to limit to its account
    getFilterOptions: function (force) {
        if (this._filterOptions && !force) {
            return this._filterOptions;
        }
        if (this.getSearchModule() == "Contacts" && this.def.name=="billing_contact_name") {
            this._filterOptions = new app.utils.FilterOptions()
                .config({
                    'initial_filter': 'filterContactsByAccountId',
                    'initial_filter_label': 'Contacts for Account',
                    'filter_populate': {
                        'account_id': this.model.get('billing_account_id'),
                    },
                    'filter_relate': {
                        'account_id': this.model.get('billing_account_id'),
                    }
                })
                .populateRelate(this.model)
                .format();
        } else {
            this._filterOptions = new app.utils.FilterOptions()
                .config(this.def)
                .setInitialFilter(this.def.initial_filter || '$relate')
                .populateRelate(this.model)
                .format();
        }
        return this._filterOptions;
    },
})

 

Is there a way I can set the filter dynamically based on whether the billing_account_id is populated?

Outcomes