FINDSET REPEAT UNTIL

Hi All,

Previously, I've got issue after looping, not all my records are modified as I forgot to use second table variable after reading topic FINDSET on other forum. However, I saw there are 2 approaches as the following:

Rec1.SETRANGE(Field1,Value1);
IF Rec1.FINDSET THEN
REPEAT
Rec2.GET(Rec1.PrimaryKey);
Rec2.Field1 = Value2;
Rec2.MODIFY;
UNTIL Rec1.NEXT = 0

Rec1.SETRANGE(Field1,Value1);
IF Rec1.FINDSET THEN
REPEAT
Rec2 := Rec1
Rec2.Field1 = Value2;
Rec2.MODIFY;
UNTIL Rec1.NEXT = 0

are both do the same thing? are both guarantee that modify will be successful 100%?
Which one is the best?



Answers

  • JuhlJuhl Member Posts: 724
    No need for 2 records variables.
    Just modify the field.
    You can also use modifyall
    Follow me on my blog juhl.blog
  • JuhlJuhl Member Posts: 724
    Rec1.SETRANGE(Field1,Value1);
    Rec1.MODIFYALL(Field1,Value2,FALSE);
    Follow me on my blog juhl.blog
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    The fastest is to use MODIFYALL as Juhl suggested.

    Among the two you have proposed the second one would be probably just a tad bit faster, I guess. The first one includes GET, which is, in theory, a relatively slow database access operation, but since you're GETting a record which has just been retrieved from the database it will be almost certainly fetched from the local cache.

    On the other hand, if you want to stick to looping and MODIFY, rather than using MODIFYALL (although looking at the sample you've posted I can's see any reason why you would not want to use MODIFYALL), each time you have to modify a field which is also used in SETRANGE or SETCURRENTKEY commands do keep using two records
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AngeloAngelo Member Posts: 180
    Hi All,

    My concern now is not about the performance but it's about modify record successfully.

    I mentioned that some records were not modified :( ,so that I try to change my code by using both approaches (use second variable). I hope MODIFYALL also give a guarantee 100% my record is modified perfectly.
  • TallyHoTallyHo Member Posts: 416
    edited 2019-04-19
    Probably you were modifying something that was affecting your loop. Modifyall does not have that problem, there is no loop.
  • RockWithNAVRockWithNAV Member Posts: 1,139
    @Slawek_Guzek - MODIFYALL is fastest to use if you are just modifying one record value but if you have multiple modifications then REPEAT UNTIL works faster then MODIFYALL.
  • AngeloAngelo Member Posts: 180
    Thanks everybody for the answer, so far my problem run smoothly by using Option 2
  • swpoloswpolo Member Posts: 80
    Also it is useful to use options in findset to speed up execution.
    FINDSET(TRUE,FALSE) in your case.
    Nav Upgrades and DEV outsourcing
    Reports transformation to RDLC
    List -1h , Complex List -3h, Document -4h (dev hours)
    navisionupgrade.com
Sign In or Register to comment.