Update Lines that are currently filtered on in repeat

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)
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.

Best Answer

Answers

  • Jan_VeenendaalJan_Veenendaal Member Posts: 206
    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 Veenendaal
  • idonavidonav Member Posts: 31
    edited 2017-05-05
    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?
  • idonavidonav Member Posts: 31
    Thanks @lubost - your suggestion solved my problem. Though - I had to remove the RESET for it to work. Thanks a lot. Will post full code tomorrow if anyone wants a complete example.
Sign In or Register to comment.