Unique bool on table

carioka
carioka Member Posts: 20
Hi
I have a bool into a table and wanted the bool could only be marked in a single row of the table
I think this:
bool onvalidate()
thistable.RESET;
thistable.SETFILTER(thistable.Dietas,'Yes');
IF thistable.FINDFIRST THEN
thistable.bool := FALSE;

but this dont run... !!! HELP !!

Comments

  • carioka
    carioka Member Posts: 20
    Sorry, my code is:

    bool onvalidate()
    thistable.RESET;
    thistable.SETFILTER(thistable.bool,'Yes');
    IF thistable.FINDFIRST THEN
    thistable.bool := FALSE;
  • mohana_cse06
    mohana_cse06 Member Posts: 5,506
    if i understood correct,u want to set bool true for only one row and if already exists in any row,u want to false that.

    am i right?:-k
  • carioka
    carioka Member Posts: 20
    if i understood correct,u want to set bool true for only one row and if already exists in any row,u want to false that.

    am i right?:-k

    Yes, if you change the bool TRUE... another row change the bool false...

    Sorry for my english

    Now, i think on this function:


    REPEAT
    IF pT_Dimension.Dietas = TRUE THEN
    pT_Dimension.Dietas := FALSE;
    UNTIL pT_Dimension.NEXT = 0;
  • kine
    kine Member Posts: 12,562
    After the change, you need to call .MODIFY to store the modification into DB... ;-)

    But:

    1) If you are filtering to boolean, do not use string, use TRUE or FALSE isntead, and use SETRANGE instead SETFILTER (this is source of your problem, that the NAV is showing error about data type). Your code will not work under another language...
    2) You are changing field, on which you are filtering - it have impact to performance and behaviour of the loop. Search for more info about this problem...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • carioka
    carioka Member Posts: 20
    kine wrote:
    After the change, you need to call .MODIFY to store the modification into DB... ;-)

    But:

    1) If you are filtering to boolean, do not use string, use TRUE or FALSE isntead, and use SETRANGE instead SETFILTER (this is source of your problem, that the NAV is showing error about data type). Your code will not work under another language...
    2) You are changing field, on which you are filtering - it have impact to performance and behaviour of the loop. Search for more info about this problem...

    I filter by SETRANGE but the problem even there
  • MBerger
    MBerger Member Posts: 413
    If your table isn't too big, you could easily do this the following way : Take a second variable of the same recordtype, select all records in the table EXCEPT the current one, then do a modifyall( boolean,false) on them
  • Sog
    Sog Member Posts: 1,023
    I'm not sure what you're trying to do.
    If you try to only have one row at each given time to have 1 or 0 rows a true value then you could try this

    bool onvalidate()
    IF bool then BEGIN //check if the new bool value is true or not
      thistable.SETFILTER("primary key field", '<>%1',rec."primary key field");
      thistable.SETRANGE(bool,true); //will skip all the records that have false, good for performance 
      if thistable.find('-') then
        thistable.modifyall(bool,false);
    end;
    

    which I now realise is the same answer as MBerger above
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • mohana_cse06
    mohana_cse06 Member Posts: 5,506
    you can try like this also.
    bool onvalidate()
    
       IF Bool THEN
       BEGIN
        thistable.SETRANGE(Bool,TRUE);
        IF thistable.FIND('-') THEN
         thistable.Bool := FALSE;
         CurrForm.Bool.UPDATE;
         thistable.MODIFY;
       END ELSE;
    
  • carioka
    carioka Member Posts: 20
    you can try like this also.
    bool onvalidate()
    
       IF Bool THEN
       BEGIN
        thistable.SETRANGE(Bool,TRUE);
        IF thistable.FIND('-') THEN
         thistable.Bool := FALSE;
         CurrForm.Bool.UPDATE;
         thistable.MODIFY;
       END ELSE;
    

    This code dont run because on the table dont have CurrForm :S
  • mohana_cse06
    mohana_cse06 Member Posts: 5,506
    carioka wrote:
    you can try like this also.
    bool onvalidate()
    
       IF Bool THEN
       BEGIN
        thistable.SETRANGE(Bool,TRUE);
        IF thistable.FIND('-') THEN
         thistable.Bool := FALSE;
         CurrForm.Bool.UPDATE;
         thistable.MODIFY;
       END ELSE;
    

    This code dont run because on the table dont have CurrForm :S

    hey sory,i have tried this on form.

    u can try without that line in table but i think the table wont update until you close it and open :-k
  • carioka
    carioka Member Posts: 20
    carioka wrote:
    you can try like this also.
    bool onvalidate()
    
       IF Bool THEN
       BEGIN
        thistable.SETRANGE(Bool,TRUE);
        IF thistable.FIND('-') THEN
         thistable.Bool := FALSE;
         CurrForm.Bool.UPDATE;
         thistable.MODIFY;
       END ELSE;
    

    This code dont run because on the table dont have CurrForm :S

    hey sory,i have tried this on form.

    u can try without that line in table but i think the table wont update until you close it and open :-k


    there is no way to update the table in real time ??
  • Sog
    Sog Member Posts: 1,023
    Have you tried the first code?
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • carioka
    carioka Member Posts: 20
    Sog wrote:
    Have you tried the first code?
    but not work
    The same happens with the other code.
    Bool is updated only when I open and close the table
  • Reinhard
    Reinhard Member Posts: 249
    It just LOOKS like the table is not updated until you open and close.
    The visual information is out of date.
    You can refresh the table by: clicking on the lines, or opening and closing the table.

    But better is to use a form: you can use statements like CurrForm.UPDATE to refresh the visual information.
  • bbrown
    bbrown Member Posts: 3,268
    The FIND is not needed if using MODIFYALL.
    There are no bugs - only undocumented features.
  • mohana_cse06
    mohana_cse06 Member Posts: 5,506
    yes,if u want to do this by form, my code works perfectly.