9 Replies Latest reply on Oct 20, 2011 9:09 PM by StevenLeuck

    Concantenate several fields into a string in another field

    StevenLeuck

      I seem to run into a scripting problem for which I need help about once a year and hope you'll forgive my inability to figure this out on my own but would appreciate some help with this one.

       

      I have a single field at the top of a form that I would like to have automatically fill in from entries in a string of fields at another location in the same document.  The string of fields are the fields where various options are types in for catalog number selections (ie, style, color, # of lamps, options, etc).  These fields will form the basis of the catalog number for the light fixture submittal I'm attempting to set up.  The single field at the top of the form would then essentially concantenate all of the individual filled in fields into a single catalog number string.  In my example I have the following fields:

       

      Cat01, Cat02, Cat03, Cat04, Cat05, Cat06..... Cat11

       

      The above fields, depending on how they might be filled in would return all of the entries with a dash between them such as: BC-232-2-UNV-GL-T8-1-EB10.

       

      I'm thinking this might not be too hard to figure out and may very well be a very simple java script.

       

      The clincher that would make this even sweeter is if the script was smart enough to know that I don't need an extra dash between fields if there is no information in any particular field.

       

      Was hoping someone out there might help me with this one for which I would be truly grateful.

        • 1. Re: Concantenate several fields into a string in another field
          try67 MVP & Adobe Community Professional

          Use this code as the Custom Calculation code of your field:

           

          var newValue = "";

          for (var i=1; i<=11; i++) {

              var fieldName = "Cat" + util.printf("%02d", i);

              var f = this.getField(fieldName);

              if (f && f.value!="") {

                  if (newValue!="")

                      newValue += "-";

                  newValue += f.value;

              }

          }

          event.value = newValue;

           

          If the number of fields you want to concatenate changes (but they still have the same name pattern), all you need to do is change the max value (currently 11) in the second line.

          • 2. Re: Concantenate several fields into a string in another field
            StevenLeuck Level 1

            Tried this but getting no return.  Tried a couple variations on the naming convention but didn't get it there either. I could email you the PDF. It's just a 2 pager so small.  I don't see here where it will allow us to upload a file so you can email me direct if you like at Steven AT ContractorsElectricLLC DOT com and I will forward it to you.  sorry to be a hassle but greatly appreciate the help I get here.

             

            SL

            • 3. Re: Concantenate several fields into a string in another field
              George_Johnson MVP & Adobe Community Professional

              This probably won't fix the script shown above, but whenever you do this type of thing, you should get the field value using the valueAsString property, as opposed to the value property. So replace every instance of "f.value" above with "f.valueAsString". This will ensure that any leading zeroes in fields with numeric entries are retained.

              • 4. Re: Concantenate several fields into a string in another field
                cuerdasg Level 1

                get rid of the util.printf stuff and just leave behind a + i and it will work fine... field wont autoupdate but you can reset it and any changes will be updated... guess you could put a this.resetForm("CONCANTENATEFIELD"); script into the custom format field of every field that updates that field that way as they get filled in the field autoupdates...

                1 person found this helpful
                • 5. Re: Concantenate several fields into a string in another field
                  gkaiseril MVP & Adobe Community Professional

                  I like to use a document level function that will concatenate 3 fields and an optional separator. If any of the 3 fields is/are a null value the script will adjust as necessary.

                   

                  // Concatenate 3 strings with separators where needed
                  function fillin(s1, s2, s3, sep) {

                  /*
                  Purpose: concatenate up to 3 strings with an optional separator

                  inputs:
                  s1: required input string text or empty string
                  s2: required input string text or empty string
                  s3: required input string text or empty string
                  sep: optional separator sting

                  returns:
                  sResult concatenated string
                  */

                  // variable to determine how to concatenate the strings
                    var test = 0; // all strings null
                    var sResult; // re slut string to return
                   
                  // force any number string to a character string for input variables
                    s1 = s1.toString();
                    s2 = s2.toString();
                    s3 = s3.toString();
                    if(sep.toString() == undefined) sep = ''; // if sep is undefined force to null

                  /*
                  assign a binary value for each string present 
                  so the computed value of the strings will indicate which strings are present
                  when converted to a binary value
                  */
                    if (s1 != "") test += 1; // string 1 present add binary value: 001
                    if (s2 != "") test += 2; // string 2 present add binary value: 010
                    if (s3 != "") test += 4; // string 3 present add binary value: 100

                    /* return appropriate string combination based on
                    calculated test value as a binary value
                    */
                    switch (test.toString(2)) {

                    case "0": // no non-empty strings passed - binary 0
                       sResult = "";
                    break;
                   
                    case "1": // only string 1 present - binary 1
                       sResult = s1;  
                    break;
                   
                    case "10": // only string 2 present - binary 10
                       sResult = s2;  
                    break;
                   
                    case "11": // string 1 and 2 present - binary 10 + 1
                       sResult = s1 + sep + s2; 
                    break;

                    case "100": // only string 3 present - binary 100
                       sResult = s3;
                    break;
                   
                    case "101": // string 1 and 3 - binary 100 + 001
                       sResult = s1 + sep + s3; 
                    break;
                   
                    case "110": // string 2 and 3 - binary 100 + 010
                       sResult = s2 + sep + s3; 
                    break;
                   
                    case "111": // all 3 strings  - binary 100 + 010 + 001
                       sResult = s1 + sep + s2 + sep + s3; 
                    break;
                   
                    default: // any missed combinations
                       sResult = "";
                    break;
                  }

                  return sResult;
                  }

                   

                  Then you can use a custom calculation script to build the necessary concatenated string by repeated calls tot he fillin function.

                   

                  With a field name rename, the calculation script could be:

                   

                  var Serial = ""; // clear computed serial number
                  var aFields = this.getField("Cat").getArray(); // get array of fields to join
                  for(i = 0; i < aFields.length; i++) {
                  Serial = fillin(Serial, aFields[i].valueAsString, "", "-");
                  }
                  event.value = Serial

                  1 person found this helpful
                  • 6. Re: Concantenate several fields into a string in another field
                    try67 MVP & Adobe Community Professional

                    get rid of the util.printf stuff and just leave behind a + i and it will work fine...

                    Bad advice. The fields are named "Cat01", "Cat02", etc, so doing it your way will not work because you'll be trying to access "Cat1", "Cat2", etc, which do not exist.

                     

                    Steven sent me the file, and my code works fine, it was just a question of changing one of the values of the fields to get it to kick into action, so to speak.

                    • 7. Re: Concantenate several fields into a string in another field
                      StevenLeuck Level 1

                      Thank you all for the help in figuring this one out.  The final fix from Try67 nailed it. I've tried several variations of catalog numbers and the final reporting field for the catalog number is reporting it right every time.  I really appreciate this.  I'll be using this command string in a number of places from now on.  Thank you!

                      Steven

                      • 8. Re: Concantenate several fields into a string in another field
                        gkaiseril MVP & Adobe Community Professional

                        As mentioned the fields were renamed, for a working example see Concatenate Fields.

                        • 9. Re: Concantenate several fields into a string in another field
                          StevenLeuck Level 1

                          THANK YOU!!  If only I could have found this file when I first went searching for a solution for this.  Thank you!