Setrange on a time field


Hi everyone,
I am using a custom table, among fields are “Starting time” and “Ending time” (both of type time).
I want to add some checks to avoid overlapping between times.
I tried to use the same code as in standard NAV table 99000752 (Shop Calendar Working Days) for validating “Starting time” and “Ending time”.
I put the following code in a function:



recXXX.SETRANGE("Starting Time",0T,"Ending Time" - 1);
recXXX.SETRANGE("Ending Time","Starting Time" + 1,235959T);
IF recXXX.FINDFIRST THEN
ERROR('Conflict!!!');



and called the function in the OnValidate triggers of both fields.
The problem is that I keep getting the following error message:



The time is not valid



The debugger stops at the first instruction.
What shall I do?
Thanks in advance :mrgreen:

Maybe try -1T instead of -1?

Hi Poppins,

You’ll get that error, if the “Starting Date” and “Ending Date” you use in your filter are not initiated before you add/deduct 1 from the value.

Hi Eric,

I am not sure I understood you well but here is the total code of my function.

recYYY.SETFILTER(“Starting Time”,‘<> %1’,0T);

recYYY.SETFILTER(“Ending Time”,‘<> %1’,0T);

IF recYYY.FINDSET THEN

REPEAT

recXXX := recYYY;

recXXX .INSERT;

UNTIL recXXX .NEXT = 0;

recXXX := xRec;

IF recXXX .DELETE THEN ;

recXXX .SETRANGE(“Starting Time”,0T,“Ending Time” - 1);

recXXX .SETRANGE(“Ending Time”,“Starting Time” + 1,235959T);

IF recXXX .FINDFIRST THEN

ERROR(‘Conflict!!!’);

As I previously said, it is a copy of the standard…but still giving error…

What do I have to change?

If you look at this line in your code:

recXXX .SETRANGE(“Starting Time”,0T,“Ending Time” - 1);

Here you specify that the “Starting Time” should use recXXX, but you do not specify which table it should take “Ending Time” from. So it will take it from the global var/table. So if this is the total of your code, then you never assign a value to “Ending Time” (as well as “Starting Time” in the following line). You never specify which record the dates are taken from. So these lines must be changed and assigned a value.

recXXX .SETRANGE(“Starting Time”,0T,WhichTableRecordAreYouTakingThisFieldFrom.“Ending Time” - 1);

Run the code and what the Ending Date/Starting Date’s used as parameters in you setrange. Then you’ll see that they are not initiated or assigned any value.

Hi Poppins,

Did this help you?