Update Lines that are currently filtered on in repeat

idonav
Member Posts: 31
Hi,
I'm trying to run the below code, though it throws an error since I need to update the lines that I am filtering on inside the Repeat statement. This causes an error saying that the record needs to be reloaded, which makes sense. But how can I accomplish the below? (I want to update each line inside the repeat)
So the update that needs to be done of no error is thrown before is:
TempLines.Status := TempLines.Status::Updated;
In the loop some records will fail, some will succeed, it is important that each record is updated with the status (or not updated). But if I put the commit outside the loop all records will be updated with the same error and status, so it 'needs' to be inside the loop.
I'm trying to run the below code, though it throws an error since I need to update the lines that I am filtering on inside the Repeat statement. This causes an error saying that the record needs to be reloaded, which makes sense. But how can I accomplish the below? (I want to update each line inside the repeat)
TempLines.SETFILTER(Status, 'Pending'); TempLines.SETFILTER("Ref Order Type", 'purchase'); TempLines.SETFILTER("Ref Order No", LinesToPost."Ref Order No"); //LinesToPost is an inparameter IF TempLines.FINDSET(TRUE, FALSE) THEN BEGIN REPEAT PurchLine.GET(PurchLine."Document Type"::Order, TempLines."Ref Order No", TempLines."Line No"); IF PurchLine."No." <> TempLines."Product Id" THEN ERROR('Error here'); PurchLine.VALIDATE("Qty. to Receive", TempLines."Qty Received"); PurchLine.MODIFY; //Is this needed after validate? TempLines.Status := TempLines.Status::Updated; IF TempLines."Processing Error" <> '' THEN TempLines."Processing Error" := ''; TempLines.MODIFY; COMMIT; UNTIL TempLines.NEXT = 0; END
So the update that needs to be done of no error is thrown before is:
TempLines.Status := TempLines.Status::Updated;
In the loop some records will fail, some will succeed, it is important that each record is updated with the status (or not updated). But if I put the commit outside the loop all records will be updated with the same error and status, so it 'needs' to be inside the loop.
0
Best Answer
-
Simply mark desired lines and after repeat .. until finishes filter MARKEDONLY records and by MODIFYALL do changes.
//TempLines.Status := TempLines.Status::Updated;
TempLines..MARK;
...
TempLines.RESET;
TempLines.MARKEDONLY(TRUE);
TempLines.MODIFYALL(Status, TempLines.Status::Updated);5
Answers
-
Simplest solution:
Create a second variable (eg. TempLines2 ) that has exactly the same type as TempLines
Instead of updating TempLines, do:TempLines2.GET( --- insert all TempLine Primary key field values here --- ); TempLines2.Status := TempLines.Status::Updated; IF TempLines2."Processing Error" <> '' THEN TempLines2."Processing Error" := ''; TempLines2.MODIFY;
You could also rewrite your code, and do it like in report 296 'Batch Post Sales Orders', using the 'IF Codeunit.RUN' construction.Jan Veenendaal0 -
Simply mark desired lines and after repeat .. until finishes filter MARKEDONLY records and by MODIFYALL do changes.
//TempLines.Status := TempLines.Status::Updated;
TempLines..MARK;
...
TempLines.RESET;
TempLines.MARKEDONLY(TRUE);
TempLines.MODIFYALL(Status, TempLines.Status::Updated);5 -
Jan_Veenendaal: I've tried that solution and unfortunately it throws the same error. (An attempt was made to change an old version of a "" record). Guess I could rewrite it, but I would if possible like to avoid it since there is another CU calling this one to trap errors.
Lubost: Good thinking, but I can't be sure that all lines should have the status Updated. It is possible that an error is thrown in the loop, if that is the case, the status should not be updated to "Updated".
EDIT: Actually, It might work as you say, since it will not be marked unless it's supposed to be updated. Will test.
To clarify, I have a another codeunit calling this one to trap errors.
(Pseudo code)Success := Codeunit.RUN(Record) IF NOT Success THEN NotTempRecord."Processing Error" := GETLASTERRORTEXT; NotTempRecord.MODIFY; COMMIT;
Any other suggestions or ideas?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