setrange with 2 fields

kanika
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???
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???
0
Answers
-
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
KB0 -
krzychub83 wrote:A solution is to add parameters to the fucntion like p_VendorNo (CODE 20), p_ItemNo (CODE 20)krzychub83 wrote:2. Try to check if you can use FINDFIRST instead of FIND('-');"Money is likewise the greatest chance and the greatest scourge of mankind."0
-
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!!0 -
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).0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions