setrange with 2 fields

kanikakanika Member Posts: 247
Hi people, firstly thanks for all

this is my problem:

I need to show on Form 7012 (purchase prices) the field "line discount%" (table "Purchase Line Discounts")

I tried to do it with a function, as other times, but in this case I need to link two fields "Vendor No." and "Item No.", tables "Purchase Price" and "Purchase Line Discounts"

this is the code:

TraeDescuento() : Decimal
WITH Descuento DO BEGIN
SETRANGE("Vendor No.","Vendor No.");
SETRANGE("Item No.","Item No.");
IF FIND('-') THEN
EXIT(Descuento."Line Discount %")
END;

where:
Descuento is a variable type "Record" subtype "Purchase Line Discount"

but it does not work, it gives me no error but it does not return data

any ideas???

Answers

  • krzychub83krzychub83 Member Posts: 120
    1. Try to specify filter values in the second parameter of the SETRANGE function. Currently your code equals to underlying lines, because the second parameters of the SETRANGE functions are set to not initialized values from the Descuentro record.

    SETRANGE("Vendor No.",'');
    SETRANGE("Item No.",'');

    A solution is to add parameters to the fucntion like p_VendorNo (CODE 20), p_ItemNo (CODE 20) and then in the code
    SETRANGE("Vendor No.",p_VendorNo);
    SETRANGE("Item No.",p_ItemNo);

    2. Try to check if you can use FINDFIRST instead of FIND('-');
    Good Luck
    KB
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    krzychub83 wrote:
    A solution is to add parameters to the fucntion like p_VendorNo (CODE 20), p_ItemNo (CODE 20)
    NAV style guide doesn't support Polish Notation. To test it just use hardcoded values and if it works check why your field based solution doesn't work.
    krzychub83 wrote:
    2. Try to check if you can use FINDFIRST instead of FIND('-');
    Depending on the records you entered in these tables and the filter you want to use, you should rather use FINDLAST.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • kanikakanika Member Posts: 247
    I just found the solution, I use form filters:

    VendNoFilter:="Vendor No.";
    ItemNoFilter:="Item No.";
    WITH Descuento DO BEGIN
    SETRANGE("Vendor No.",VendNoFilter);
    SETRANGE("Item No.",ItemNoFilter);
    IF FIND('-') THEN
    EXIT(Descuento."Line Discount %")
    END;

    Thanks for all!!
  • krzychub83krzychub83 Member Posts: 120
    I am sorry, I haven't explained it clear enough at the beginning. Initial bug was related to the WITH statement. The WITH statement can be used to change which variable is currently in the 'spotlight'. By default it is Rec variable (on many objects), however you can modify it with the WITH statement. In a result you do not need to use this variables name inside of the WITH statement, because NAV compiler will do it for you.
    So in your example the WITH statement changed the "Vendor No." and "Item No." to be taken from the Descuento and not from the current record. Your initial code was like this for the compiler:
    WITH Descuento DO BEGIN
    Descuento.SETRANGE("Vendor No.",Descuento."Vendor No.");
    Descuento.SETRANGE("Item No.",Descuento."Item No.");
    IF Descuento.FIND('-') THEN
    EXIT(Descuento."Line Discount %")
    END;
    

    So you could resolve this issue by changing your code to something similar to this:
    Descuento.SETRANGE("Vendor No.",Rec."Vendor No."); 
    Descuento.SETRANGE("Item No.",Rec."Item No.");
    IF Descuento.FIND('-') THEN
    EXIT(Descuento."Line Discount %")
    

    In this example I have directly told to the NAV to use values from the Rec variable. Try to always use variable names inside the code, especially if you are not sure of your code. To be honest I would advise it even if you are sure of your code. I have seen a lot of bugs done this way by developers with years of experience.

    I would advice you to check how NAV debugger works. This will help you a lot to understand what is happening inside of your code.

    Try to consider einsTeIn.NET advice too. Do you need the first record or maybe the last one from that table?

    At the end I would advice you to have a look here: http://www.mibuso.com/howtoinfo.asp?FileID=22. This tutorial will point you to few other aspects, like missing RESET function, FINDFIRST/FINDLAST functions or SETCURRENTKEY (which is required if you work on SumIndexFields, or you don't use SQL, or if you need to get records in specific order under the SQL. If you use SQL then in case of your code you don't need to worry about it. SQL will handle ti for you).
Sign In or Register to comment.