Problem with REPEAT loop

poppins
Member Posts: 647
Hi everyone,
I am having an issue with the following code:
What's missing in this code?
Thanks in advance
I am having an issue with the following code:
Prefixe:='FCM'; SalesInvoiceHeader.RESET; SalesInvoiceHeader.SETRANGE("Posting Date",DateFilter,WORKDATE); IF SalesInvoiceHeader.FINDSET THEN BEGIN REPEAT SalesInvoiceHeader.RENAME(Prefixe+ COPYSTR(SalesInvoiceHeader."No.",3,STRLEN(SalesInvoiceHeader."No."))); UNTIL (SalesInvoiceHeader.NEXT = 0); END;The problem is that the code doesn't loop, it modifies only one record.
What's missing in this code?
Thanks in advance

0
Comments
-
Hi poppins,
You're trying to rename "Sales Invoice Header" with a PK field ("No."). [-X
Try to change your current key to sort documents without PK or use a temporary table to do that (renaming).0 -
amazan wrote:Hi poppins,
You're trying to rename "Sales Invoice Header" with a PK field ("No."). [-X
Try to change your current key to sort documents without PK or use a temporary table to do that (renaming).
Can you explain a little more?0 -
Try changing the code to:
IF SalesInvoiceHeader.FINDSET(TRUE, TRUE) THEN BEGIN
Maybe you will go away without understanding what amazan has written before0 -
radek.bb wrote:Try changing the code to:
IF SalesInvoiceHeader.FINDSET(TRUE, TRUE) THEN BEGIN
Maybe you will go away without understanding what amazan has written before
Yes, you should do that but it's not the whole solution. You need to use a different record variable for the rename than you are using for the looping. Accomplish this by usign 1 record variable for the looping. Then, as you retreived each record, copy it to a second variable and do the rename.There are no bugs - only undocumented features.0 -
poppins wrote:I am not sure I understand what you've just said...
Can you explain a little more?
Yes, I can.
You're trying to rename a bunch of invoices using Primary Key (field "No.") so when you're in renaming records, the position of the cursor changes depending of new value of your documents,.
For example:
First Loop
Invoice 'A' <- Cursor (Invoices 'A' becomes to 'FRZA')
Invoice 'B'
Invoice 'C'
After renaming
Invoice 'B'
Invoice 'C'
Invoice 'FRZA' <- Cursor
Next step it will be "end of loop" because the position of the cursor is the end of the selected records and can't find another record to do RENAME.0 -
amazan wrote:poppins wrote:I am not sure I understand what you've just said...
Can you explain a little more?
Yes, I can.
You're trying to rename a bunch of invoices using Primary Key (field "No.") so when you're in renaming records, the position of the cursor changes depending of new value of your documents,.
For example:
First Loop
Invoice 'A' <- Cursor (Invoices 'A' becomes to 'FRZA')
Invoice 'B'
Invoice 'C'
After renaming
Invoice 'B'
Invoice 'C'
Invoice 'FRZA' <- Cursor
Next step it will be "end of loop" because the position of the cursor is the end of the selected records and can't find another record to do RENAME.
so, what shall I do to correct it?0 -
bbrown wrote:You need to use a different record variable for the rename than you are using for the looping. Accomplish this by usign 1 record variable for the looping. Then, as you retreived each record, copy it to a second variable and do the rename.0
-
amazan wrote:poppins wrote:I am not sure I understand what you've just said...
Can you explain a little more?
Yes, I can.
You're trying to rename a bunch of invoices using Primary Key (field "No.") so when you're in renaming records, the position of the cursor changes depending of new value of your documents,.
For example:
First Loop
Invoice 'A' <- Cursor (Invoices 'A' becomes to 'FRZA')
Invoice 'B'
Invoice 'C'
After renaming
Invoice 'B'
Invoice 'C'
Invoice 'FRZA' <- Cursor
Next step it will be "end of loop" because the position of the cursor is the end of the selected records and can't find another record to do RENAME.mohana_cse06 wrote:bbrown wrote:You need to use a different record variable for the rename than you are using for the looping. Accomplish this by usign 1 record variable for the looping. Then, as you retreived each record, copy it to a second variable and do the rename.Prefixe:='FCM'; SalesInvoiceHeader.RESET; SalesInvoiceHeader.SETRANGE("Posting Date",DateFilter,WORKDATE); IF SalesInvoiceHeader.FINDSET THEN BEGIN REPEAT SalesInvoiceHeader1.RESET; IF SalesInvoiceHeader1.GET(SalesInvoiceHeader."No.") THEN BEGIN ModifEcrituresClientDetaillees(SalesInvoiceHeader1."No.",Prefixe); ModifEcrituresClient(SalesInvoiceHeader1."No.",Prefixe); ModifEcrituresComptables(SalesInvoiceHeader1."No.",Prefixe); ModifEcrituresTVA(SalesInvoiceHeader1."No.",Prefixe); SalesInvoiceHeader1.RENAME(Prefixe+ COPYSTR(SalesInvoiceHeader."No.",3,STRLEN(SalesInvoiceHeader."No."))); END; UNTIL (SalesInvoiceHeader.NEXT = 0); END;
but it is still wrong.....it keeps looping then gives me the following error message:Overflow under type conversion of Text to Code. Value: FCMMMMMMMMMMM+1303767
I think it keeps looping over the first invoice....
What shall I do?0 -
poppins wrote:...I think it keeps looping over the first invoice....
What shall I do?
How about this?mohana_cse06 wrote:bbrown wrote:You need to use a different record variable for the rename than you are using for the looping. Accomplish this by usign 1 record variable for the looping. Then, as you retreived each record, copy it to a second variable and do the rename.There are no bugs - only undocumented features.0 -
Is it a one time job?
What about adding a filter on No.?SalesInvoiceHeader.RESET; SalesInvoiceHeader.SETRANGE("No.",<first record>,<last record>); //New line SalesInvoiceHeader.SETRANGE("Posting Date",DateFilter,WORKDATE); IF SalesInvoiceHeader.FINDSET THEN BEGIN REPEAT SalesInvoiceHeader1 := SalesInvoiceHeader; SalesInvoiceHeader1.RENAME('FCM' + SalesInvoiceHeader1."No."); UNTIL SalesInvoiceHeader.NEXT = 0;
Please test it in TEST Environment.0 -
bbrown wrote:You need to use a different record variable for the rename than you are using for the looping. Accomplish this by usign 1 record variable for the looping. Then, as you retreived each record, copy it to a second variable and do the rename.
That's what I did with my second code...
I used one variable for loop (SalesInvoiceHeader) and then inside the loop, I used a second variable (SalesInvoiceHeader1.GET(SalesInvoiceHeader."No.")) to do the rename....
Correct me if I am wrong :-k0 -
Take a look at amazan's example again. Now the cursor does not follow along with the renamed record anymore, but the renamed record still is placed at some position within the very set of records you are processing.
But, you are about to rename posted invoices and modify entries. It is quite likely that what you try to do is illegal, or at the least, not good practice.0 -
poppins wrote:bbrown wrote:You need to use a different record variable for the rename than you are using for the looping. Accomplish this by usign 1 record variable for the looping. Then, as you retreived each record, copy it to a second variable and do the rename.
That's what I did with my second code...
I used one variable for loop (SalesInvoiceHeader) and then inside the loop, I used a second variable (SalesInvoiceHeader1.GET(SalesInvoiceHeader."No.")) to do the rename....
Correct me if I am wrong :-k
error should be from inside functions within the loop. Please check in debugger.aav!o0 -
Use SalesInvoiceHeader.Ascending(False).
This will change all ur records.0 -
abhinav0408 wrote:Use SalesInvoiceHeader.Ascending(False).
This will change all ur records.
Mind you, this will not change any records. This *might* lead to your code successfully doing what you want *for a particular data set* you're processing, but this is no general solution.0 -
vaprog wrote:abhinav0408 wrote:Use SalesInvoiceHeader.Ascending(False).
This will change all ur records.
Mind you, this will not change any records. This *might* lead to your code successfully doing what you want *for a particular data set* you're processing, but this is no general solution.0 -
Test it with a FINDSET(TRUE, TRUE)There are no bugs - only undocumented features.0
-
poppins wrote:bbrown wrote:Test it with a FINDSET(TRUE, TRUE)
You're right, it wouldn't. The issue is your renamed records fall with in the filter range. One approach would be to initially mark the records you need to change and then only process those marked records.There are no bugs - only undocumented features.0 -
1 - Copy all the records into a temporary record variable.
2 - loop through the temporary records.
3 - for each temporary record, GET the corresponding 'real' record and RENAME that record as necessary0 -
Dear,
I think the problem is the "Posting Date" did not found the filter record you use with DateFilter variable and WORKDATE,
since "Posting Date" is Date type only.0 -
FINDSET and REPEAT does not make sense. Use FINDSET for modifyall. If you use REPEAT for looping start with the first FINDFIRST0
-
search1110 wrote:FINDSET and REPEAT does not make sense. Use FINDSET for modifyall. If you use REPEAT for looping start with the first FINDFIRST0
-
DenSter wrote:search1110 wrote:FINDSET and REPEAT does not make sense. Use FINDSET for modifyall. If you use REPEAT for looping start with the first FINDFIRST
And MODIFYALL does not require any retrieval statement. No GET, FIND, FINDSET, etc is needed before a MODIFYALL or DELETEALL.There are no bugs - only undocumented features.0 -
Have you solved this problem? this is simple me thinks..
first, your filter not run correctly, test it with count function to check how many record you get.
There's something wrong with your DateFilter, except all the codes, everything looks fine..0 -
To all those of you, who still could not figure out the root cause of the problem here, and are giving all kind of useless suggestions:
When you want to modify records in a loop, you need to take care that you do not skip any records nor re-process any that you processed already. The problem is most prevalent with RENAME, but not restricted to it.
Skipping may occur, when you modify the data of the record variable on which you call NEXT (or FIND('>'). These same circumstances may also lead to reprocessing, depending on the nature of the change to the variable.
Reprocessing may additionally occur if the modified record is placed somewhere in the set of records to be processed at a position after the current one.
Different kind of methods can be applied in order to prevent the problems.
Probably the most robust is the one suggested by DenSter: create a temporary list of records to be processed which you do not modify during procession. Changing the sorting order or applying suitable filters might be others, depending on circumstances.
While FINDSET(TRUE,TRUE) appears to be a solution, it is not. The parameters are meant to optimize calls to the database server, not to solve the problem discussed here.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