13 Replies Latest reply on Dec 19, 2016 9:15 AM by philmodjunk

    Script Trigger to Mask a Time Field?

    brian.curran

      Hi all,

      I'm using a script trigger to mask a Date field which works perfecty.

       

      Date Mask

      #This script converts dates entered as DDMMYY and DDMMYYYY with no delimitters and converts them into FileMaker dates

      when the OnObjectValidate trigger is tripped.

      Set Variable [ $FieldRef; Value:Get ( ScriptParameter ) ]

      If [ not IsValid ( GetAsDate ( GetField ( $FieldRef ) ) ) ]

      Set Variable [ $DateText; Value:Filter ( GetAsText ( GetField ( $FieldRef ) ) ; 9876543210 ) // Get just the numeric characters

      entered. ]

      If [ Let ( $L = Length ( $DateText ) ; $L = 6 or $L = 8 ) // correct number of digits entered ]

      Set Field By Name [ $FieldRef; Let ( Y = Right ( $DateText ; $L - 4 ) ;

      GetAsDate ( Left ( $DateText ; 2 ) & "/" & Middle ( $DateText ; 3 ; 2 ) & "/" & If ( Length ( Y ) = 2 ; 2000 ) + Y )

      ) ]

      End If

      End If

       

       

      However, I'm trying to adapt the above script to use on a "Time" field but I can't get it to work. Please see below:

       

      Time Mask

      #This script converts times entered as hhmm with no delimiters and converts them into FileMaker times when the

      OnObjectValidate trigger is tripped.

      Set Variable [ $FieldRef; Value:Get ( ScriptParameter ) ]

      If [ not IsValid ( GetAsTime ( GetField ( $FieldRef ) ) ) ]

      Set Variable [ $TimeText; Value:Filter ( GetAsText ( GetField ( $FieldRef ) ) ; 9876543210 ) // Get just the numeric characters

      entered. ]

      If [ Let ( $L = Length ( $TimeText ) ; $L = 4 ) // correct number of digits entered ]

      Set Field By Name [ $FieldRef; GetAsTime ( Left ( $TimeText ; 2 ) & ":" & Right ( $TimeText ; 2 ) ) ]

      End If

      End If

       

      If I enter 0330 in the field with the Script Debugger and Data Viewer open, it skips several lines and goes straight to the last "End If" line. I can't use an 'Auto-enter Calculated value', as I'm using GoZync and things are not working correctly with that type of Mask. Any suggestions?

       

      Many thanks

      Brian.

        • 1. Re: Script Trigger to Mask a Time Field?
          Mike_Mitchell

          Hi, Brian.

           

          Your problem is the "IsValid" test. Take a look at these dialogs:

           

          dialog.png

           

          dialog2.png

           

          GetAsTime ( "0330" ) evaluates as 330:00:00. In FileMaker's time calculations, that's a valid time. It's the 330th hour, zero minutes, zero seconds. It doesn't make logical sense to us humans, but FileMaker says it's X seconds since zero time, which is a valid time.

           

          To make this work right, you need to change it to check for delimiters and then insert them, if missing, using the various text parsing functions.

           

          HTH

           

          Mike

          • 2. Re: Script Trigger to Mask a Time Field?
            mikebeargie

            If your script debugger is skipping everything inside your "IF" statement. Then there's something wrong with:

             

            If [ not IsValid ( GetAsTime ( GetField ( $FieldRef ) ) ) ]

             

            Something in there is making it return a boolean of 1 and skipping the IF actions completely (which would make it jump to "END IF" in debugger).

             

            So a few things to consider based on what you've said:

             

            1) Is that $fieldref field a time field? IsValid only validates on field type (IE date, time, timestamp)

            2) Doesn't filemaker already auto-mask time data input as regular numbers?

               a) In which case, would you change it to just mask all values to your desired format? IE drop the "IF"

            3) Also are you adding seconds in for a valid time format? ( IE 03:30:00 )

            • 3. Re: Script Trigger to Mask a Time Field?
              brian.curran

              Thanks Mike, this is becoming a bit of a headache but I appreciate your help

              • 4. Re: Script Trigger to Mask a Time Field?
                DrewTenenholz

                Brian --

                 

                After fixing the problem Mike highlights, I would suggest that for broadest compatibility, you use:

                 

                Date ( month ; day ; year )

                and

                Time ( hours ; minutes ; seconds )

                 

                Instead of GetasDate ( text & "/" & text & "/" ; text ) and GetAsTime ( text & ":" & text )

                 

                That will make these calculations work even on a machine running under a different localization scheme (e.g. European vs. American date/time formats).

                 

                And I did notice that you filter out everything but digits, but that doesn't make the calculation engine see them as numbers unless you explicitly use GetAsNumer ().  The string expressions for Left(), Middle(), and Right() are actually returning text strings.  I know it works, and everyone would expect it to, but computers can be so darn literal sometimes, that your result is getting tripped up by this sort of thing.

                 

                So, I guess that your users can be expected to reliably enter 04082013 and not just 4813 for 8 April 2013?  I rarely see users sticking to such a computer-compatible method.

                 

                -- Drew

                • 5. Re: Script Trigger to Mask a Time Field?
                  brian.curran

                  Hi Mike, that makes more sense the way you have explained the jump to the final End If.

                   

                  1) Yes, $FieldRef is a Time field.

                  2) Not sure what you mean but I'll try dropping the If and an End If from the script.

                  3) No seconds, just two digits for the Hour and two digits for the Minute (military time/24-hour)

                   

                  Thanks

                  Brian.

                  • 6. Re: Script Trigger to Mask a Time Field?
                    mikebeargie

                    If you go to an unvalidated/untriggered time field in browse mode and type in 0330, it will default mask it to 3:30 AM. You can set the field display settings to any combination of HHMMSS, and 12/24 hour time. When you click back in the field you will see it go back to 0330

                     

                    IE filemaker "masks" the format of the data, but does not change the data itself.

                     

                    My method has always been to use standard filemaker validation for checking if it's time (trains the USER to always use the correct format). But I can see where trigger based masking is useful.

                    • 7. Re: Script Trigger to Mask a Time Field?
                      Mike_Mitchell

                      Mike -

                       

                      I'm not certain that's correct. Here's a time field with "0330" entered into it:

                       

                      dialog.png

                       

                      Here's how it looks when you commit:

                       

                      dialog2.png

                       

                      According to the Help, when you enter a single digit (no delimiters) FileMaker treats it as the hour:

                       

                      help.png

                       

                      So, if I divide 330 by 24 and take the remainder, I get 18 - which is 6 PM. The 330th hour corresponds to 6 PM, or 1800 hours.

                       

                      You're correct that it will retain the original number (minus the leading zero), but the number is interpreted as "hours", not "hours plus minutes". And I agree - you're probably best off to use validations, but I've had situations where folks are addicted to military time and just don't get the whole "colon" thing.   

                       

                      Cheers.

                       

                      Mike

                      • 8. Re: Script Trigger to Mask a Time Field?
                        mikebeargie

                        hmm, I could have sworn I've experienced different behaviour on a field that shows HH:MM A/PM as the format, but testing it out I see that you are indeed correct.

                        • 9. Re: Script Trigger to Mask a Time Field?
                          brian.curran

                          Thanks for everyone's input, I managed to get it working exactly as I needed just by deleting the "notIsValid" element.

                           

                          Time Mask

                          #This script converts times entered as hhmm with no delimiters and converts them into FileMaker times when the

                          OnObjectValidate trigger is tripped.

                          Set Variable [ $FieldRef; Value:Get ( ScriptParameter ) ]

                          If [ not IsValid GetAsTime ( GetField ( $FieldRef ) ) ]

                          Set Variable [ $TimeText; Value:Filter ( GetAsText ( GetField ( $FieldRef ) ) ; 9876543210 ) // Get just the numeric characters

                          entered. ]

                          If [ Let ( $L = Length ( $TimeText ) ; $L = 4 ) // correct number of digits entered ]

                          Set Field By Name [ $FieldRef; GetAsTime ( Left ( $TimeText ; 2 ) & ":" & Right ( $TimeText ; 2 ) ) ]

                          End If

                          End If

                           

                          Thanks again...

                          Brian.

                          • 10. Re: Script Trigger to Mask a Time Field?
                            tonizd

                            Hi,

                            When I try to put Set Field By Name [ $FieldRef; GetAsTime ( Left ( $TimeText ; 2 ) & ":" & Right ( $TimeText ; 2 ) ) ] then I get this message: an operator is expected here.. What is wrong?

                            • 11. Re: Script Trigger to Mask a Time Field?
                              philmodjunk

                              What is highlighted when you get that message?

                               

                              Did you type in that semi-colon? (;) --You don't type that in. The semi-colon is the divider between the expression you enter for the target field name calculation ($FieldRef) and the expression that computes the calculated result (GetAsTime ( Left ( $TimeText ; 2 ) & ":" & Right ( $TimeText ; 2 ) ) ). There's a different button to click for each of these two parameters and you don't actually type in that semi-colon. FileMaker adds it for you after you close the dialogs where you entered the expressions.

                              • 12. Re: Script Trigger to Mask a Time Field?
                                tonizd

                                Highlighted part is ; GetAsTime ( Left ( $TimeText ; 2 ) & ":" & Right ( $TimeText ; 2 ) )

                                I don't know how to write codes, srcipts etc. I thought that I can type all this lines in the script and it will work. Well thank you for your help

                                • 13. Re: Script Trigger to Mask a Time Field?
                                  philmodjunk

                                  Failure to correctly specify the parameters of the set field is a common "newbie" issue. The highlight confirms that you entered the semi-colon instead of switching to the next section to enter the calculation shown after the semi-colon.