Your problem is the "IsValid" test. Take a look at these dialogs:
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.
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 )
Thanks Mike, this is becoming a bit of a headache but I appreciate your help
After fixing the problem Mike highlights, I would suggest that for broadest compatibility, you use:
Date ( month ; day ; year )
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.
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)
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.
I'm not certain that's correct. Here's a time field with "0330" entered into it:
Here's how it looks when you commit:
According to the Help, when you enter a single digit (no delimiters) FileMaker treats it as the hour:
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.
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.
Thanks for everyone's input, I managed to get it working exactly as I needed just by deleting the "notIsValid" element.
#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
If [ Let ( $L = Length ( $TimeText ) ; $L = 4 ) // correct number of digits entered ]
Set Field By Name [ $FieldRef; GetAsTime ( Left ( $TimeText ; 2 ) & ":" & Right ( $TimeText ; 2 ) ) ]
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?
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.
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
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.