4 Replies Latest reply on May 3, 2017 12:38 PM by steves40082172

    Creating a Control Number by concatenating fields

    steves40082172

      Can someone help me with a JavaScript question?  I'd like to create a new field which I'll call Control Number.  This field would be comprised of the first four letters in "Last Name" combined with the current date to create what would amount to a 10 digit field (NNNNDDDDDD).  This is a low volume form, so I'm not concerned about the potential for a duplicate control number.  It is essentially for reference purposes.

        • 1. Re: Creating a Control Number by concatenating fields
          George_Johnson MVP & Adobe Community Professional

          What should happen if the last name field is blank or has fewer than four characters?

          • 2. Re: Creating a Control Number by concatenating fields
            steves40082172 Level 1

            My inclination would be to follow the path of least resistance.  Ideally, the name portion of the field would be filled with asterisks or some other special character.  Spaces would be OK if that makes it easier, but I would prefer to have a visible character that could be recorded on another document.

             

            Basically, this is going to be an order form for a certificate of deposit that could be renewed multiple times.  The control number will be manually entered into a descriptive field associated with the CD and carried with it through the renewal process so we can tie it back to the original order form.  For a variety of reasons, CD numbers change when a CD is renewed or reissued, so we can't link the order form to the first CD number. 

             

            I hope this helps, and I thank you for taking interest in a JavaScript question from a novice.

            • 3. Re: Creating a Control Number by concatenating fields
              George_Johnson MVP & Adobe Community Professional

              Here is a custom Validate script you can use in the Last Name field. It uses the characters from the last name field and converts them to upper case and uses a date format of yyyymmdd. You can change this to suit. The name of the control number field is assumed to be "Control Number", so change that if you're using a different field name.

               

              // Custom Validate script for Last Name field

              (function () {

               

                  // Get a reference to the control number field

                  var fCN = getField("Control Number");

               

                  // If this field is blank, blank the Control Number field

                  if (!event.value) {

                      fCN.value = "";

                      return;

                  }

               

                  // Determine the prefix for the CN field

                  var sPrefix = (event.value + "***").substr(0, 4).toUpperCase();

               

                  // Determine the date string

                  var sDate = util.printd("yyyymmdd", new Date());

               

                  // Set the Control Number field value

                  fCN.value = sPrefix + sDate;

               

              })();

               

               

              This script will be triggered whenever the Last Name field value is changed. If you want it to happen at some other time, post again with more info.

              • 4. Re: Creating a Control Number by concatenating fields
                steves40082172 Level 1

                Amazing!  Thank you for such an elegant solution.  It worked perfectly!