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?
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;
Comments
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.
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.
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?
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.
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com