Can't access fields in view controller

I'm having a problem accessing field values in a view controller. I have a controller for the Resources record view that takes a value from a dropdown and sets the value of a field depending on the dropdown selection. It looks like this:

// custom/modules/Resources/clients/base/views/record/record.js

extendsFrom: 'RecordView',

({
initialize: function (options) {
   this._super('initialize', [options]);
   this.model.once("sync", this.assignOnChangeFunctions, this);
},

assignOnChangeFunctions: function () {
   this.model.on('change:custom_rate', this.custom_rate_changed, this);
},

custom_rate_changed: function() {
   console.log('project_manager_l2_rate_c: ' + this.model.get('project_manager_l2_rate_c'));
   console.log('project_manager_l3_rate_c: ' + this.model.get('project_manager_l3_rate_c'));
   switch(this.model.get('custom_rate')) {
      case 'Project Manager L2':
         this.model.set('resource_rate_actual', this.model.get('project_manager_l2_rate_c'));
      break;
      case 'Project Manager L3':
          this.model.set('resource_rate_actual', this.model.get('project_manager_l3_rate_c'));
      break;
   }
},
})

Controller for the create view looks just like the above, except it extends from CreateView and instead of this.model.once("sync", this.assignOnChangeFunctions, this) I have this.assignOnChangeFunctions. The create controller works as expected. But the record controller can't access the values of project_manager_l2_rate_c and project_manager_l3_rate_c fields. The output from console.log statements is "undefined". But, I can see the values of these fields in the database. When I try to access other fields in this Resources record, the results are mixed. Some of the fields can be accessed and others cannot. I don't see any pattern for which fields can be accessed. It seems to make no difference whether a field is in resources or resources_cstm table. Replacing this.model.once("sync", this.assignOnChangeFunctions, this) with this.assignOnChangeFunctions makes no difference. What else can I try to troubleshoot this?

  • Hi Yury Voloshin,

    You can try with the following code and see if that helps.

    // custom/modules/Resources/clients/base/views/record/record.js
    ({
       extendsFrom: 'RecordView',
       initialize: function (options) {
          this._super('initialize', [options]);
          //this.model.once("sync", this.assignOnChangeFunctions, this);
          this.model.on('change:custom_rate', this.custom_rate_changed, this);
       },

       custom_rate_changed: function() {
          //if undefined, return
          if(!_.isUndefined(this.model.get('custom_rate'))) {
             console.log('project_manager_l2_rate_c: ' + this.model.get('project_manager_l2_rate_c'));
             console.log('project_manager_l3_rate_c: ' + this.model.get('project_manager_l3_rate_c'));
             switch(this.model.get('custom_rate')) {
                case 'Project Manager L2':
                   this.model.set('resource_rate_actual', this.model.get('project_manager_l2_rate_c'));
                break;
                case 'Project Manager L3':
                    this.model.set('resource_rate_actual', this.model.get('project_manager_l3_rate_c'));
                break;
             }
          }
       },
    })

    Also, regarding your query on what fields can be accessible within the view controller:

    When I try to access other fields in this Resources record, the results are mixed. Some of the fields can be accessed and others cannot. I don't see any pattern for which fields can be accessed.

    You can only access the fields which are declared/available in the view metadata file eg. record.php in your case.

    For fields which you do want to show on record view but would like to keep it in metadata for some other logic calculation, you can declare those fields inside related_fields array like below, taken from leads record.php file.

    Regards.

  • Thank you hats! The problem was that the new field was not in viewdefs. I was able to access it in the view controller as soon as I added it to the record view. This is something new I learned about Sugar.