Options

How to change previous record?

Hi
I want to change previous record in table (to set ending if date in current record is today). I try this code:

OnAfterGetRecord()
IF "Date From"=TODAY THEN BEGIN
Rec.NEXT:=-1;
Rec."Date To":=TODAY-1;
END;

Answers

  • Options
    zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    edited 2017-02-08
    You can access previous record by doing
    Rec.NEXT(-1); //it will return you the previous record.
    

    General syntax is Rec.NEXT(Steps);


    please verify my answer, if it was helpful for you. thanks.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • Options
    RockWithNAVRockWithNAV Member Posts: 1,139
    Why dint you used MODIFY at last??
  • Options
    KishormKishorm Member Posts: 921
    edited 2017-02-11
    You should do it on a copy of the record, not on Rec itself. So create a variable called Rec2 for the same table and then try the following code...
    OnAfterGetRecord()
    IF "Date From" = TODAY THEN BEGIN
      Rec2.COPY(Rec);
      IF Rec2.NEXT(-1) THEN BEGIN
        Rec2."Date To" := TODAY-1;
        Rec2.MODIFY;
      END;
    END;
    

    EDIT: Oops - @jreynolds is correct, code amended accordingly :)
  • Options
    jreynoldsjreynolds Member Posts: 175
    Be careful using assignment (:=) to copy Rec to Rec2. This will only copy the values of the fields in Rec to Rec2; it will not copy settings for filters and the current key. Therefore, Rec.NEXT(-1) and Rec2.NEXT(-1) will not necessarily be the same record. It's better to use COPY so that filters and key settings are copied as well.
Sign In or Register to comment.