AnsweredAssumed Answered

Multiselect field with dynamic values

Question asked by jfernandez on Jul 23, 2019

I think it is important to start by saying that I am in an instance with a SugarCRM Professional 9.0.0.

 

I have an Account with a multiselect field called "Account Materials". Also I have a Meetings with a multiselect field called "Meeting Materials". Both fields have the same values "materials_list".

 

When I create a Meeting and relate it to an Account, the "Meeting Materials" field only must allow select the the values who have the "Account Materials" field in the related account. In the same way, if I edit the Meeting, only must be available the "Account Materials" values of the related Account in the "Meeting Materials" field.

 

That is, the meeting materials field should only let you select those materials from the related account and you cannot do this until a related account has been selected so you have to dynamically "filter" the values that should appear.

 

I have this code from Angel Martinez Oct 19, 2017 9:40 AM (adapted):

({
    extendsFrom: 'RecordView',
    initialize: function (options) {
        this._super('initialize', [options]);
        this._filterSalesTipoSoporteValues();
        this.model.on('change:parent_name', this._filterSalesTipoSoporteValues, this);
    },
    _filterSalesTipoSoporteValues: function () {
        debugger;
        var self = this;
        var accountBean = app.data.createBean('Accounts', {id: self.model.get("parent_id")});
        var materials_list = app.lang.getAppListStrings('materials_list');

        var request = accountBean.fetch({
            success: _.bind(function (data) {
                if (accountBean.get("account_materials_c")) {
                    inString = (accountBean.get("account_materials_c")).toString();
                    var dd_values = {};
                    var dd_array = {};
                    var array_count = 0;
                    _.each(materials_list, function (value, key) {
                        if (inString.search(key) != -1) {
                            dd_values[key] = value;
                            dd_array[array_count] = key;
                            array_count = array_count + 1;
                        } else {
                            delete dd_values[key];
                        }
                    });
                    self.model.fields['meeting_materials_c'].options = dd_values;
                    if (Object.keys(dd_values).length) {
                        self.model.set('meeting_materials_c', inString);
                    }
                }
            }, this),
        });
    },
});

 

But this script don't delete the not allowed values from the original list and, although sets the values from the parent account,  don't save them when push the save button.

 

I think the problem are the "Multiselect" field type, if it was be a dropdowns I'm sure that it would works. Has anyone encountered this situation before? How did you solve it?

Outcomes