How to get code 9 rather that code 10

Gabry
Member Posts: 48
This code wants to re-create a new code starting from the last and highest one that is already present in AddressTable. The problem is that when it arrives code 10 (code, [10] lenght), FINDLAST gets 9 code and not the last one 10. When I try to insert a new record with primary key (Customer, Code) there is unique record problem.
I need a statement which gets the highest code between 9 and 10: probably is a lenght problem. How can I manage that?
I need a statement which gets the highest code between 9 and 10: probably is a lenght problem. How can I manage that?
AddressTable.SETRANGE("Customer No.", CustomerNo); IF AddressTable.FINDLAST THEN BEGIN // get last record CodeMax := AddressTable.Code; pCode := FORMAT(INCSTR(CodeMax), 10); END ELSE BEGIN pCode := FORMAT('1', 10); END;
0
Comments
-
Make sure that the length of all Customer No's is the same by adding 0's in front (00001, 00009, 00010, 00011).
Or add a field to the table of type Integer, copy the Customer No. to that field and use SETCURRENTKEY on that field. Then you are sorting on a integer-field instead of a code-field, which results in the correct sort order.No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)0 -
Luc Van Dyck wrote:Make sure that the length of all Customer No's is the same by adding 0's in front (00001, 00009, 00010, 00011).
I tried to apply the above solution using FORMAT(code, 10), but it does not work.
Do you know how to add the suitable number of zeros (code lenght is [10]) before the number (of different lenghts: e.g. 9, 10, 100)?
I checked on format properties as the 3.rd parameter of FORMAT function, but so far I did not find it.0 -
FOR i := 1 TO 10 - STRLEN("Customer No.") DO "Customer No." := '0' + "Customer No."
No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)0 -
You could use: CONVERTSTR(FORMAT(number,10),' ','0'))0
-
Kishorm wrote:You could use: CONVERTSTR(FORMAT(number,10),' ','0'))
Thank you. For those codes that I am able to write by myself it works. The issue is that some these codes (1,..., 9, 10, ..., 100) are already written in the database, thus using FINDLAST I do not get the record with the highest code.
Please do you have any other insights using the read capabilities/statement only?0 -
If you want to keep the existing values as they are then there are a couple of options...
1) Assuming you haven't already started using values prefixed with 0s then you can set the "SQL Data Type" property of the Code field to Integer - this will mean that the values are stored as an Integer in SQL (and hence sorted by numeric value) and you will not be able to use any non-numerical chars but assuming that the code is only generated by your small code snippet as per the 1st post then this will be fine.
2) An alternative option, assuming that each customer will not have too many addresses, is to iterate through each of their address, use EVALUATE(IntegerVariable, Code) to convert the Code field to an Integer variable and then keep a record of the highest Integer value found - and then use the next one.0 -
The magic is in the property string for the FORMAT command...
AddressTable.SETRANGE("Customer No.", CustomerNo); IF AddressTable.FINDLAST THEN BEGIN // get last record CodeMax := AddressTable.Code; pCode := FORMAT(INCSTR(CodeMax), 10,'<Integer,10><Filler Character,0>'); END ELSE BEGIN pCode := FORMAT('1', 10,'<Integer,10><Filler Character,0>'); END;
________________________________
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com0
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