How to make a field visible based on a formula

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • murugappan
    Active Community Member
    • Aug 2017
    • 467

    How to make a field visible based on a formula

    Hi, I need help to make a field visible or read-only based on a formula. How can i do that? Example, I want to calculate the age of a person and based on the age i want activate a field called "body weight". We need get the body weight of a person who is less than 12 years of age.

    Please help.
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Hi murugappan you can do that with the dynamic handler feature https://github.com/espocrm/documenta...mic-handler.md a formula won't work because you need to make the changes in the front end and a formula is a back end feature similar to a hook that's executed just before or after an entity has been saved.

    Comment

    • esforim
      Active Community Member
      • Jan 2020
      • 2204

      #3
      Couldn't you do it with Dynamic Logic? Here is some example:

      The issue is to do the Formula, not sure how you will be able to do that calculation.

      Click image for larger version  Name:	image_4159.png Views:	4 Size:	579.4 KB ID:	56887
      Click image for larger version  Name:	Dynamic Logic 2png.png Views:	0 Size:	6.4 KB ID:	56889
      PS: in the example I used "Required field", your case you want to use "Read Only"
      Attached Files
      Last edited by esforim; 03-18-2020, 07:52 AM.

      Comment

      • murugappan
        Active Community Member
        • Aug 2017
        • 467

        #4
        Hi guys,

        There is an example of the calculated field but that does not help as it works only when a record is saved.

        I will explore the BPM further to see how this can be done. I need the guys in espocrm to help me with what function will help me to trigger field visibility and read-only. I could not find this in formula documentation. I will surely post the solution here once i get it working.

        Comment

        • Maximus
          Senior Member
          • Nov 2018
          • 2731

          #5
          Hi murugappan ,

          > but that does not help as it works only when a record is saved.
          You don't need fire 'Save' to see changes. Just input age -> press Enter -> the dependent field should appear.

          Comment

          • esforim
            Active Community Member
            • Jan 2020
            • 2204

            #6
            Yes, I can confirm Maximus is right, you don't need to save for these two field to activate "Show" and activate "Read Only". After inputting the Age field it will be visible.

            I believe the issue you are having it, you want the age to be calculate instantly after you type in birthday.

            The issue with Formula is, it won't work until you click that Save button. To fix your issue, I think you need to use what telecastg said, "dynamic handler feature". Good luck with that though, I never used it. There might a few thread around, documentation might have some example, etc.

            Comment

            • telecastg
              Active Community Member
              • Jun 2018
              • 907

              #7
              Hi murugappan

              I don't know if there are any plans to implement dynamic-handler using the Administration GUI, since it is designed to make possible the use of highly flexible customized parameters (like hiding or showing a field based on the value of another field or even a value of a field in a different but linked entity) to improve data entry, but an easy to follow example is provided in the documentation link that I posted above.



              Applying that documentation example to your case, this is how I would implement a dynamic handler to make a field called "weight" appear in a Contact entity when field "age" in the same entity value is under 12:

              1) Add new fields "age" type "int", and "weight" type "int" to entity "Contact" in Administration > Entity Manager > Contact > Fields > Add Field

              2) Open the script: custom/Espo/Custom/Resources/metadata/clientDefs/Contact.json (created automatically by Espo when you modified the out of the box Contact entity) and add the following code to specify that the entity has a dynamicHandler:
              Code:
              {
                "dynamicHandler": "custom:contact-dynamic-handler"
              }
              3) Create a new script: client/custom/src/contact/contact-dynamic-handler.js
              (For those not familiar with using code instead of just using GUI to customize Espo, notice how the name of this script matches the name of the "dynamicHandler" value specified in step 2 above and that the script is stored in the "custom" namespace)
              Code:
              define('custom:contact-dynamic-handler', ['dynamic-handler'], function (Dep) {
              
                return Dep.extend({
              
                  init: function() {
              
                    // execute method controlFields
                    this.controlFields();
              
                    // invoke the method  controlFields every time the value of field "age" changes
                    this.recordView.listenTo(
                      this.model, 'change:age', this.controlFieds.bind(this)
                    );
              
                  },
              
                  // define method controlFields
                  controlFields: function () {
              
                    // display the field "weight" only when the value of "age" is less than 12
                    if(parseInt(this.model.get('age')) < 12) {
                      this.recordView.showField('weight');
                    } else {
                      this.recordView.hideField('weight');
                    }
              
                  }
              
              
                });
              });
              Last edited by telecastg; 03-19-2020, 06:15 PM.

              Comment

              Working...