this.model in quote-data-group-list does not have relate field from record.php

Hello,

I've been struggling to figure this one out and wondered if anyone had any ideas. We have a requirement to make a relationship between products and Tax Rates, selectable in the 7.9+ Sidecar quote-data-group-list.php view.

Here's a brief outline of what I've done:

I have this vardef in custom/Extension/modules/Products/Ext/Vardefs/file.php

$dictionary['Product']['fields']['vat_rate'] = array(
     'name' => 'vat_rate',
     'vname' => 'LBL_LIST_VAT_RATE',
     'type' => 'id',
     'required'=>false,
     'do_report'=>false,
     'reportable'=>false,
);
$dictionary['Product']['fields']['vat_rate_name'] = array(
    'name' => 'vat_rate_name',
    'rname' => 'name',
    'id_name' => 'vat_rate',
    'join_name' => 'taxrates',
    'type' => 'relate',
    'link' => 'taxrates',
    'table' => 'taxrates',
    'isnull' => 'true',
    'module' => 'TaxRates',
    'dbType' => 'varchar',
    'len' => '255',
    'vname' => 'LBL_LIST_VAT_RATE',
    'source' => 'non-db',
    'comment' => 'Vat Rate Name',
    'massupdate' => false,
);

I've copied quote-data-group-list.php to custom from modules/Products/clients/base/views/quote-data-group-list.php and added this just before the total_amount array:

array(
    'name' => 'vat_rate_name',
    'type' => 'quote-vat-relate',
    'initial_filter' => 'active_taxrates',
    'filter_populate' => array(
        'module' => array('TaxRates'),
    ),
    'related_fields' => array(
        'vat_rate_name',
        'vat_rate',
        'taxrates'
    ),
),

You'll see I have a custom field type called quote-vat-relate, which I've mocked from the product template selector, extending the BaseRelateField type, located at custom/modules/Products/clients/base/fields/quote-vat-relate.js

({
    extendsFrom: 'BaseRelateField',

    format: function(value) {
        var idList;
        value = value || this.model.get('vat_rate_name');

        this._super('format', [value]);

        if (value) {
            idList =  this.model.get('vat_rate');
            if (_.isArray(value)) {
                this.formattedIds = idList.join(this._separator);
            } else {
                this.formattedIds = idList;
            }

            if (_.isEmpty(this.formattedIds)) {
                this.formattedIds = value;
            }
        }

        return value;
    },
})

This all seems to be going well as when I edit the items, I can select a VAT code, and I can confirm it stores in the database. Great!

Working

Navigate, and come back, and it's disappeared!

When I dug into this, I found that when the format() function is initially invoked, this.model doesn't contain my field. Even stranger is that when I move the product lines around, it invokes format() again, and the value suddenly displays! When I looking at the value of this.model now, it contains all of the attributes, including vat_rate_name now!

So it's seems to be down to an initial display issue. Can I ensure that this field is included in this.model from the start? Is this a bug?

Any suggestions greatly appreciated.

  • Hi Francesca,

    Blast... If that's the "correct" way, then it doesn't work. What it does if I add my "vat_rate_name" field in there, is to not display any lines at all.

    Must be a limitation of using relate fields, or perhaps there's something wrong with the way the relationship is being defined. I suspect it's because the "related fields" are relate fields themselves.

    It may just not be possible then... Thanks anyway!

    Richard

  • That is very curious, I assume you checked your logs to see if there are any errors when the lines disappear?

  • Francesca Shiekh wrote:

    That is very curious, I assume you checked your logs to see if there are any errors when the lines disappear?

    Hi Francesca,

    I had checked my logs, and found what I thought to be nothing (it was full of unrelated relationship errors). But upon closer inspection, there was an invalid link to my taxrates relationship. I only saw an error clearly returned to me when I made an API call to Products separately.

    After having a closer look, I realised that I'd never defined the TaxRates side of the relationship, or the actual link definition.

    I've added the following:

    custom/modules/Quotes/clients/base/views/record/record.php

    //.....
    'name' => 'product_bundle_items',
        'fields' => array (
    //.....
    vat_rate,
    vat_rate_name,
    ),
    //.....

    This is now custom/Extension/modules/Products/Ext/Vardefs/file.php

    $dictionary['Product']['fields']['vat_rate'] = array(
         'name' => 'vat_rate',
         'vname' => 'LBL_LIST_VAT_RATE',
         'type' => 'id',
         'required'=>false,
         'do_report'=>false,
         'reportable'=>false,
    );
    $dictionary['Product']['fields']['vat_rate_name'] = array(
        'name' => 'vat_rate_name',
        'rname' => 'name',
        'id_name' => 'vat_rate',
        'join_name' => 'vat_rate_products',
        'type' => 'relate',
        'link' => 'vat_rate_products',
        'table' => 'taxrates',
        'isnull' => 'true',
        'module' => 'TaxRates',
        'dbType' => 'varchar',
        'len' => '255',
        'vname' => 'LBL_LIST_VAT_RATE',
        'source' => 'non-db',
        'comment' => 'Vat Rate Name',
        'massupdate' => false,
    );

    $dictionary['Product']['fields']['vat_rate_products'] = array(
        'name' => 'vat_rate_products',
        'type' => 'link',
        'relationship' => 'vat_rate_products',
        'vname' => 'LBL_LIST_VAT_RATE',
        'source' => 'non-db',
    );
    $dictionary['TaxRate']['relationships']['vat_rate_products'] = array(
        'lhs_module' => 'TaxRates',
        'lhs_table' => 'taxrates',
        'lhs_key' => 'id',
        'rhs_module' => 'Products',
        'rhs_table' => 'products',
        'rhs_key' => 'vat_rate',
        'relationship_type' => 'one-to-many',
    );

    custom/Extension/modules/TaxRates/Ext/Vardefs/file.php

    $dictionary['TaxRate']['relationships']['vat_rate_products'] = array(
        'lhs_module' => 'TaxRates',
        'lhs_table' => 'taxrates',
        'lhs_key' => 'id',
        'rhs_module' => 'Products',
        'rhs_table' => 'products',
        'rhs_key' => 'vat_rate',
        'relationship_type' => 'one-to-many',
    );

    Thanks for your persistence with checking there was an error, and for linking to that document. I've never come across that in my searching so it definitely helped.

    Thanks again!

    Richard

  • Glad you were able to figure it out Richard Coleman! Thank you Francesca Shiekh!

    App Ecosystem @ SugarCRM