Dynamic field values: enum, multi-enum, etc.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandtank
    Active Community Member
    • Mar 2017
    • 379

    Dynamic field values: enum, multi-enum, etc.

    Is it possible to provide dynamic values for fields other than links? Being able to provide a dynamic list of options for an enum or multi-enum field, for example, would be very useful in my environment.
  • alter
    Member
    • Apr 2018
    • 57

    #2
    Hi, not sure what do you mean by dynamic values other than links (probably that "opportunity" link is actually "opportunityId" and "opportunityName" field behind the scenes) ?

    Comment

    • telecastg
      Active Community Member
      • Jun 2018
      • 907

      #3
      Hello bandtank

      Yes it is possible to determine the options offered in an enum or multi-enum field via javasript coding.

      To see an actual example, check this code: https://github.com/espocrm/espocrm/b...pe-list.js#L41

      There you can see how the options for the multi-enum field entity-type-list are populated by fetching a list of scopes defined in metadata.

      Don't forget though, if you are using Espo 7.2, that the field validation "enhancement", will throw an error unless the metadata "options" definition for your field is set to null.

      Comment

      • bandtank
        Active Community Member
        • Mar 2017
        • 379

        #4
        Originally posted by telecastg
        Hello bandtank

        Yes it is possible to determine the options offered in an enum or multi-enum field via javasript coding.

        To see an actual example, check this code: https://github.com/espocrm/espocrm/b...pe-list.js#L41

        There you can see how the options for the multi-enum field entity-type-list are populated by fetching a list of scopes defined in metadata.

        Don't forget though, if you are using Espo 7.2, that the field validation "enhancement", will throw an error unless the metadata "options" definition for your field is set to null.
        This helps a lot. Thanks. I will make an API call to get the list of multi-enum options instead of fetching the values from metadata. It's basically the same thing, though.

        Comment

        • telecastg
          Active Community Member
          • Jun 2018
          • 907

          #5
          You're welcome

          Here's a sample of using an Ajax (API) call to load a multi-enum field with a list of roles, that I use in the Chats extension:

          Code:
                  setupOptions: function () {
                      Espo.Ajax.getRequest('Role/action/list').then(
                          function (data) {
                              data.list.sort((a, b) => (a.name > b.name) ? 1 : -1);
                              this.params.options = [];
                              data.list.forEach(function(optionObj){
                                  // skip non-user roles for internal chat notifications
                                  if(this.name.indexOf('privateChat') > -1 && (optionObj.userPermission === 'no' || optionObj.userPermission === 'not-set') ) {
                                      return;                            
                                  }
                                  this.params.options.push(optionObj.name);                        
                              },this);
                              // update the field display after downloaing the options from the database
                              this.reRender();
                          }.bind(this)
                      );
                  }
          ​
          Here's another sample of loading a multi-enum with the names of the fields defined in an entity's list layout. This from the Grouped Records extension:

          Code:
                  setup: function() {
                      Dep.prototype.setup.call(this);
                      // halt processing to allow the layout manager call to generate a response
                      this.wait(true);
                      this.setupOptions();
                  },
          
                  setupOptions: function () {
                      // use the 'list' layout to determine the fields that can be chosen to filter the collection
                      this._helper.layoutManager.get(this.model.get('entityType'), 'list', (listLayout) => {
                          let layoutArray = [];
                          listLayout.forEach(function(field){
                              layoutArray.push(field.name);
                          });
                          this.params.options = layoutArray;
                          this.wait(false);
                      },this);  
                  }
          ​
          Last edited by telecastg; 10-14-2022, 09:22 PM.

          Comment

          • bandtank
            Active Community Member
            • Mar 2017
            • 379

            #6
            Wait, a chat extension? Did you release this or is it internal/private? Either way, that's a cool idea and thank you for the code. That is exactly what I want to do.

            Comment


            • telecastg
              telecastg commented
              Editing a comment
              You're welcome.

              Here's the link for the Chats extension: https://payhip.com/b/tq62X

              It is tested up to 7.1.11 only because I don't want to invest time in trying to make it work with 7.2 until that version is mature enough and the field validation bugs have been fixed, but it should work even with 7.2 on its current state.

            • bandtank
              bandtank commented
              Editing a comment
              That's awesome. Thanks for the effort and link.
          Working...