9 Replies Latest reply on Oct 6, 2016 12:12 PM by robert.a.marsh

    Javascript Calculations on Spawned Pages from Template

    robert.a.marsh Level 1

      I have a form that has 5 pages that allows the addition of pages from templates. None of the calculations or auto tabs work. I'm very new to javascript and have searched many forums without success. Here is what my age script looks like, what do I need to add so when it spawns from the template it works properly. Thanks in advance for help.

       

      var dob = util.scand("mm/dd/yyyy", this.getField("P3.AttachmentA.VIC2DOB").valueAsString);

      if (dob != null) {

          var today = new Date();

          var age = today.getFullYear() - dob.getFullYear();

          var m = today.getMonth() - dob.getMonth();

          if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

              age--;

          }      

          event.value = age;

      }

      else {

          event.value = "";

      }

        • 1. Re: Javascript Calculations on Spawned Pages from Template
          gkaiseril MVP & Adobe Community Professional

          If you are renaming your fields as you spawn the pages, you calculation scripts need to pickup the "P#." prefix on the spawned pages and adjust the field names in your calculations as needed.

          • 2. Re: Javascript Calculations on Spawned Pages from Template
            robert.a.marsh Level 1

            How do I enter the "P#." into the script, I've tried several different ways without success.

            • 3. Re: Javascript Calculations on Spawned Pages from Template
              gkaiseril MVP & Adobe Community Professional

              Does your templates have calculations in the form fields?

               

              Have you checked to see if there are calculations for the fields on any of the spawned pages?

               

              One can get the name of a field using the "event.target.name". For a spawned page, one to can use the "split(".")" to create and array of the name's strings between the ".".

              • 4. Re: Javascript Calculations on Spawned Pages from Template
                robert.a.marsh Level 1

                Can you provide an example of what that would look like. I only need for the above script and auto tab, thanks

                • 5. Re: Javascript Calculations on Spawned Pages from Template
                  gkaiseril MVP & Adobe Community Professional

                  When a template spawns a new page with the field raname parameter set to "true" all the fields on the spawned page are prefixed with a string of values like "("P3.AttachmentA.". So a field with a name of "VIC2DOB"  after page 2 using template "AttachmentA" gets a name of "P3.AttachmentA.VIC2DOB".

                  It is possible to split a stirng variable into an array of string values if there is a seperator character between the strings like "." to split any renamed field into an array. For a spawned field with the name "P3.AttachmentA.VIC2DOB" can be split into an array of 3 elements using the "split" method.

                  var aFieldName = "P3.AttachmentA.VIC2DOB".split(".");
                  for(var i = 0; i < aFieldName.length; i++)
                  {
                  console.println("element " + i + " = " + aFieldName[i]);
                  }

                  Creates the following output:

                  element 0 = P3
                  element 1 = AttachmentA
                  element 2 = VIC2DOB

                  true

                  One can use element 0 and element 1 to create the added prefix string for the renamed fields on the spawned page.


                  // get the focuesed field's name;
                  var cFieldName = event.target.name;
                  // split hireachial values into elements of an array;
                  var aFieldName = cFieldName.split(".");
                  // check to see if the calculation is on the template page;
                  if(aFieldName.length > 2) {
                  // element 0 of field name array is the P#. element 1 of the array is the template name from which the field has been spawned;
                  var cFieldPrefix = aFieldName[0] + "." + aFieldName[1] + ".";

                  var dob = util.scand("mm/dd/yyyy", this.getField(cFieldPrefix + "VIC2DOB").valueAsString);
                  if (dob != null) {
                      var today = new Date();
                      var age = today.getFullYear() - dob.getFullYear();
                      var m = today.getMonth() - dob.getMonth();
                      if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
                          age--;
                      }      
                      event.value = age;
                  }
                  else {
                      event.value = "";
                  }
                  else
                  {
                  // cmpute using the field names of the template;
                  var dob = util.scand("mm/dd/yyyy", this.getField("VIC2DOB").valueAsString);
                  if (dob != null) {
                      var today = new Date();
                      var age = today.getFullYear() - dob.getFullYear();
                      var m = today.getMonth() - dob.getMonth();
                      if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
                          age--;
                      }      
                      event.value = age;
                  }
                  else {
                      event.value = "";
                  }
                  } // end field name not on template page;

                  • 6. Re: Javascript Calculations on Spawned Pages from Template
                    robert.a.marsh Level 1

                    I've tried the above, keep getting syntax error, not sure that I'm putting scripts in right place. Thank You Again!!

                    I need guidance on where to place codes.

                    • 7. Re: Javascript Calculations on Spawned Pages from Template
                      robert.a.marsh Level 1

                      The template coding is:

                       

                      var dob = util.scand("mm/dd/yyyy", this.getField("VIC2DOB").valueAsString);

                      if (dob != null) {

                          var today = new Date();

                          var age = today.getFullYear() - dob.getFullYear();

                          var m = today.getMonth() - dob.getMonth();

                          if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

                              age--;

                          }     

                          event.value = age;

                      }

                      else {

                          event.value = "";

                      }

                       

                      The Javascript I originally inquired about was actually from the paged spawned from the template with added Javascript to calculate DOB. The original Template has no prefixes added. Hope this help and again all your help is appreciated.

                      • 8. Re: Javascript Calculations on Spawned Pages from Template
                        gkaiseril MVP & Adobe Community Professional

                        From a working example, the script for the calculation on the template page for the age field:

                         

                        var cPreFix = "";
                        // get prefix elements for spawned pages;
                        var aFieldName = event.target.name.split(".");
                        if(aFieldName.length > 2)
                        {
                        // first 2 elements of name array makeup the prefix for fields on a spawned page;
                        cPreFix = aFieldName[0] + "." + aFieldName[1] + ".";
                        }
                        // compute age in years;
                        var dob = util.scand("mm/dd/yyyy", this.getField(cPreFix + "VIC2DOB").valueAsString);
                        if (dob != null) {
                            var today = new Date();
                            var age = today.getFullYear() - dob.getFullYear();
                            var m = today.getMonth() - dob.getMonth();
                            if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
                                age--;
                            }
                            event.value = age;
                        }
                        else {
                            event.value = "";
                        }

                         

                        Your script does not suppress the "0" value when there is no entry for the date of birth field.

                        1 person found this helpful
                        • 9. Re: Javascript Calculations on Spawned Pages from Template
                          robert.a.marsh Level 1

                          Works perfect, Thank You and I was able to supress the "0" thanks again for all your help.