Findlast not getting the correct record

catiamatos1991catiamatos1991 Member Posts: 158
edited 2018-11-14 in NAV Three Tier
So I need to change my previous record (if it exists) of Sales Price.
When I insert a new record I need to fill the "Ending Date" field.

But my problem now is I tried this code
IF "Sales Price".INSERT(TRUE) THEN BEGIN
  "Sales Price".RESET;
  IF "Sales Price".FINDLAST THEN BEGIN
    "Sales Price".NEXT(-1);
    MESSAGE('data 2 %1 ',"Sales Price"."Starting Date");
    "Sales Price"."Ending Date":=DataInicio-1;
    "Sales Price".MODIFY(TRUE);
  END;

But the previous record is not being modified... And the message shows the a wrong previous record like the image below...

xyxn8pfv9tv3.png



I need to fill the field Ending Date right next to the date 08-11-2018 to close the last product price. In this case it should have the value (12-11-2018) but is not being filled for some reason.



My idea is when inserting a new record I always need to fill the Ending Date of my previous record. If I'm modifying a record already inserted in the system, the idea is to modify the existing record understand?

Answers

  • Developer101Developer101 Member Posts: 528
    Instead of going previous record the better way is to find the sales price again with new instance and then update the ending date.
    you could do
    if salesprice.insert then
    salespriceenddate.setfilters (new instance of sales price table- set the filters to get the line where you want to update the ending date)
    if salespriceenddate.findfirst then
    salespriceenddate.ending date:=DataInicio-1;
    salespriceenddate.modify



    United Kingdom
  • catiamatos1991catiamatos1991 Member Posts: 158
    edited 2018-11-14
    My confusion is how I'm going to pass the filter I'm going to pass? I can't get the right last Starting Date with find last...
  • Developer101Developer101 Member Posts: 528
    sorry:

    if salesprice.insert then
    salespriceenddate.setfilters (new instance of sales price table- set the filters to get the line where you want to update the ending date)
    if salespriceenddate.findlast then
    salespriceenddate.ending date:=DataInicio-1;
    salespriceenddate.modify

    pass the ending date not blank filter

    United Kingdom
  • catiamatos1991catiamatos1991 Member Posts: 158
    My problem is I can't get the previous line to change to set the filter. I understand your code
  • Developer101Developer101 Member Posts: 528
    dont find the previous line. filter the table as you would do from the start. I do not understand where the issue is.
    United Kingdom
  • catiamatos1991catiamatos1991 Member Posts: 158
    edited 2018-11-14
    IF "Sales Price".INSERT(TRUE) THEN BEGIN
      "Sales Price".RESET;
      "Sales Price".SETRANGE("Sales Price"."Item No.",PRODUTO); 
      "Sales Price".SETRANGE("Sales Price"."Sales Type",1);
      "Sales Price".SETRANGE("Sales Price"."Sales Code",'ALL');
      "Sales Price".SETRANGE("Sales Price"."VAT Bus. Posting Gr. (Price)",'NAC');
      "Sales Price".SETRANGE("Ending Date",0D); 
      "Sales Price".SETRANGE("Currency Code",''); 
      "Sales Price".SETRANGE("Variant Code",'');
      IF "Sales Price".FINDLAST THEN BEGIN
         MESSAGE('data 1 %1 ',"Sales Price"."Starting Date");
        "Sales Price"."Ending Date":=DataInicio-1;
        "Sales Price".MODIFY(TRUE);
      END;
    END ELSE BEGIN
      "Sales Price".MODIFY(TRUE);
    END;
    

    This will fill the Ending Date of my current Line and not the previous one.. SO the price instead of 10.95 should be 12.65 (unit Price)

    breiqmmji0xa.png

  • Developer101Developer101 Member Posts: 528
    Just add another filter on Setfilter (starting date, <>%1 , sales price."starting date")
    United Kingdom
  • catiamatos1991catiamatos1991 Member Posts: 158
    edited 2018-11-14
    I've used this and worked.. What you think? Is not a right way? I Know that i want always the previous line of what i'm inserting so I add this "Sales Price".NEXT(-1); to my code
    IF "Sales Price".INSERT(TRUE) THEN BEGIN
      "Sales Price".RESET;
      "Sales Price".SETRANGE("Sales Price"."Item No.",PRODUTO); 
      "Sales Price".SETRANGE("Sales Price"."Sales Type",1);
      "Sales Price".SETRANGE("Sales Price"."Sales Code",'ALL');
      "Sales Price".SETRANGE("Sales Price"."VAT Bus. Posting Gr. (Price)",'NAC');
      "Sales Price".SETRANGE("Ending Date",0D); 
      "Sales Price".SETRANGE("Currency Code",''); 
      "Sales Price".SETRANGE("Variant Code",'');
      IF "Sales Price".FINDLAST THEN BEGIN
         MESSAGE('data 1 %1 ',"Sales Price"."Starting Date");
        "Sales Price".NEXT(-1);
        "Sales Price"."Ending Date":=DataInicio-1;
        "Sales Price".MODIFY(TRUE);
      END;
    END ELSE BEGIN
      "Sales Price".MODIFY(TRUE);
    END;
    

    And worked...

    q27t53gem951.png


  • Developer101Developer101 Member Posts: 528
    by the way i would have done different , i would have closed the last active sales price line and then insert the new price
    United Kingdom
  • catiamatos1991catiamatos1991 Member Posts: 158
    BUt i've dont what you said,
    if salesprice.insert then
    salespriceenddate.setfilters (new instance of sales price table- set the filters to get the line where you want to update the ending date)
    if salespriceenddate.findlast then
    salespriceenddate.ending date:=DataInicio-1;
    salespriceenddate.modify
    
  • Developer101Developer101 Member Posts: 528
    thats fine if worked i was just only wanted to work for you what you already had started but if i was doing this i would have closed the sales price first and then insert new price.
    United Kingdom
  • catiamatos1991catiamatos1991 Member Posts: 158
    Hmm ok, thanks for the idea though
  • krikikriki Member, Moderator Posts: 9,094
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • RockWithNAVRockWithNAV Member Posts: 1,139
    Use SETCURRENTKEY You will get the record that you expecting!!
  • Developer101Developer101 Member Posts: 528
    yes you are correct
    United Kingdom
Sign In or Register to comment.