Problem with Create-Actions Dynamic Views (different views based on user)

While the solution described here works well for record views/inline edit and editing existing records
http://cheleguanaco.blogspot.com/2014/08/sugarcrm-customization-dynamic.html

I can't seem to apply the same concept to the creation of new records.
The drawer opens with the default record.php layout before the initialize function has completed...
create-actions.js custom code:
               
({
  extendsFrom: 'CreateActionsView',
  initialize: function(options){
    this._super('initialize', [options]);
      this._super('initialize', [options]);
      var self = this;
      //set the view depending on user
      var userBean = app.data.createBean('Users', {id: app.user.id});
      var request = userBean.fetch();
      request.xhr.done(function(){
         var u_support_group = userBean.get('support_group_c') ;
         self.meta = app.metadata.getView(self.module, 'record-cs');
         if (u_support_group == 'TS' || u_support_group =='TSJ' || u_support_group =='TSS'){
            self.meta = app.metadata.getView(self.module, 'record-ts');
         }else{
            self.meta = app.metadata.getView(self.module, 'record-cs');
         }
      });
  },
  render: function()
  {
    this._super('render');
    this.setBusinessUnit();
  },
  setBusinessUnit: function(){
    var self = this;
    var userBean = app.data.createBean('Users', {id: app.user.id});
    var request = userBean.fetch();
    request.xhr.done(function(){
       var u_support_group= userBean.get('support_group_c') ;
       self.model.set('case_department_c',u_support_group);
    });
  },
  _dispose: function() {
    this._super('_dispose');
  },
})
                                                                                               


also tried extending instead of replacing

self.meta = _.extend({}, app.metadata.getView(self.module, 'record-ts'), self.meta);

Any ideas?

FrancescaS
  • Hi FrancescaS, 

    I want to know if you find an answer for this, or how you did it?

    I'm having the same problem.

    Thanks.
  • Yes, sorry for not updating this.
    I was able to add my custom support_group_c to the fields that are fetched  when the user logs in, this means I can just use
           

    var u_support_group = app.user.get('support_group_c');

    And I don't have to wait for the User record to be retrieved to get that value.

    To fetch my custom field I extended the CurrentUserApi creating:

    custom/clients/base/api/CustomCurrentUserApi.php

    <?php

            

    require_once 'clients/base/api/CurrentUserApi.php';

    class CustomCurrentUserApi extends CurrentUserApi

    {

        /**

         * Override the core api with our own versoin of retrieveCurrentUser

         */

        public function registerApiRest()

        {

            return array(

                'retrieve' => array(

                    'reqType' => 'GET',

                    'path' => array('me',),

                    'pathVars' => array(),

                    'method' => 'retrieveCurrentUser',

                    'shortHelp' => 'CustomWR: Returns current user info including custom info',

                    'longHelp' => 'include/api/help/me_get_help.html',

                    'ignoreMetaHash' => true,

                    'ignoreSystemStatusError' => true,

                ),

            );

        }

        /**

         * Extends retrieveCurrentUser and add our custom data

         */

        public function retrieveCurrentUser($api, $args)

        {

            $user_data_array = parent::retrieveCurrentUser($api, $args);

            global $current_user;

            $user_data_array['current_user']['support_group_c'] = $current_user->support_group_c;

            $user_data_array['current_user']['business_unit_c'] = $current_user->business_unit_c;

            return $user_data_array;

        }

    }

    ?>


    HTH

    FrancescaS

  • Hi FrancescaS,

    I will try myself and see how it works. 

    Thank you very much-
  • Glad you got a solution, but have you tried:

      extendsFrom: 'CreateActionsView',

      initialize: function(options){

        this._super('initialize', [options]);

          this._super('initialize', [options]);

          var self = this;

          //set the view depending on user

          var userBean = app.data.createBean('Users', {id: app.user.id});

          var request = userBean.fetch();

          request.xhr.done(function(){

             var u_support_group = userBean.get('support_group_c') ;

             self.meta = app.metadata.getView(self.module, 'record-cs');

             if (u_support_group == 'TS' || u_support_group =='TSJ' || u_support_group =='TSS'){

                self.meta = app.metadata.getView(self.module, 'record-ts');

             }else{

                self.meta = app.metadata.getView(self.module, 'record-cs');

             }

    render: function()

      {

        this._super('render');

        this.setBusinessUnit();

      },

      setBusinessUnit: function(){

        var self = this;

        var userBean = app.data.createBean('Users', {id: app.user.id});

        var request = userBean.fetch();

        request.xhr.done(function(){

           var u_support_group= userBean.get('support_group_c') ;

           self.model.set('case_department_c',u_support_group);

        });

      },

      _dispose: function() {

        this._super('_dispose');

      },

          });

      },

    })