AnsweredAssumed Answered

Changes in Sidecar model not reflected in Sugar bean

Question asked by Yury Voloshin on Mar 5, 2020
Latest reply on Mar 16, 2020 by Yury Voloshin

I have a requirement where a field called resource_rate_actual has be set equal to one of several fields, depending on the value selected in a dropdown. I've set it up in record.js view controller like this:

 

initialize: function (options) {
        this._super('initialize', [options]);
        this.model.on('change:resource_type', this.resource_type_changed, this);
    }, 
resource_type_changed: function(){
        if(!_.isUndefined(this.model.get('resource_type'))) {
            switch (this.model.get('resource_type')) {
                case 'Project Manager':
                    this.model.set('resource_rate_actual', this.model.get('project_manager_rate_c'));
                    break;
                case 'Engineer':
                    this.model.set('resource_rate_actual', this.model.get('engineer_rate_c'));
                    break;
            }
        }
    },

 

This works well to automatically set the resource_rate_actual field on the front end, depending on whether a Project Manager or an Engineer is selected in the dropdown. But, when the record was saved, the value in the resource_rate_actual field did not get saved to the database. In order to save it, I had to set up a before_save logic hook to set the value of $bean->resource_rate_actual on the back end with this code:

switch ($bean->custom_rate) {
                case 'Project Manager':
                    $bean->resource_rate_actual = $bean->project_manager_rate_c;
                break;
                case 'Project Manager':
                    $bean->resource_rate_actual = $bean->project_manager_rate_c;
                    break;

}

With this logic hook, the record was saved as displayed on the front end.

My question is, why is it necessary to set the value of resource_rate_actual both on the front end and on the back end? As I understand it, a Sidecar model is a JS object that represents the Sugar Bean, and any changes to the model should be reflected in the bean. Is this not correct?

Outcomes