3 Replies Latest reply on Dec 14, 2016 9:23 AM by summerside19036174

    Custom calculation script for field that has value but not entered by user

    summerside19036174

      Hello,

       

      I'm creating an interactive form where users fill out details and I am using various scripts which I found online. I'm not very proficient with JavaScript so I'm hoping someone can help me.

       

      One feature of the form is a summary section that populates with the answers the user has given earlier in the form. To facilitate this I am using the following script:

       

      var n1 = this.getField("Project name");
      var n2 = this.getField("Summary_Project name");
      n2.value = n1.value;
      

       

      So whatever is entered in the field "Project name" is automatically filled in in "Summary_Project name". This is activated by an On Blur action which works as the user is entering text into the parent field.

       

      The next tool I am using is Form Router Calendar tool which uses a whole bunch of JavaScript that I don't understand - but allows the user to pick a date from a calendar picker. This works by adding a button next to the "Date" field.

       

      The problem I face is because text is not directly entered in the "Date" field, the script mentioned above does not work to copy the date to the Summary section.

       

      So I thought there could be a way to use a custom calculation script for the "Date" field that says something along the lines of "If this field has a value, then copy it's contents to the "Summary_Date" field". I'm hoping that this could work in theory so once the date picker adds the date to that field, it will automatically copy it to the Summary field.

       

      As mentioned I have no knowledge of writing code or the proper syntax needed so I hope somebody can help. I really appreciate any advice you can give.

       

      Thank you!

       

      Harry.

        • 1. Re: Custom calculation script for field that has value but not entered by user
          Karl Heinz Kremer Adobe Community Professional

          Don't use the on-blur action to copy data, use a calculation script instead. Let's say you have to fields Field1 and Field2, and somehow the information in Field1 changes - either because it is changed by a user directly, or because some other script modifies it (e.g. your calendar picker). Any change in the form will trigger a calculation event, that is sent to all fields that have a calculation script. When you look at your field properties dialog, you will notice a "Calculate" tab:

           

          2016-12-14_10-54-58.png

           

          When you select a custom calculation script, you can enter JavaScript code to calculate the value of this field. It's value gets changed when you modify the property "event.value" - the "event" is an object that is sent to the calculation script with information about this specific event (e.g. which field triggered it, but also the value that the field should be set to).

           

          So, if you want to copy data from Field1, you can use the following script:

           

          event.value = this.getField("Field1").value;
          

           

          You are using a "push" approach - the value gets pushed from the field that changed to other fields that need that information. A calculation script uses a "pull" approach: It pulls information from other fields when something in the form changed. There are pros and cons for each approach. In this case, the calculation script is the easier way to implement this.

          • 2. Re: Custom calculation script for field that has value but not entered by user
            summerside19036174 Level 1

            This worked great and makes a lot of sense.

             

            Thank you!

            • 3. Re: Custom calculation script for field that has value but not entered by user
              summerside19036174 Level 1

              Hi Karl, thanks for your help.

               

              I have another question off the back of that. On of my fields is a phone number so I would like the user to enter '01234567890' (10 digits) but in the summary field the first zero doesn't appear. Similarly if I type '000000000' (zeros) into the parent field I only get one '0' in the summary field. Would this be because of the calculation field?

               

              thanks