Codeunit Transaction problem

tobarichtobarich Member Posts: 33
Hi all, i have created a codeunit which generates Purchase Orders. The informaiton of the Orders is read form a table.
The problem is that, if the first record of the table can not generate an order for some reason the codeunit stops its execution and does not generate the rest of the Orders.
I try to put a COMMIT after the generation but this line is never read, the error finish the execution before.
IF g_recVenInvNo.FIND('-') THEN
REPEAT
BEGIN
  f_CreateOrder();

  COMMIT;
END
UNTIL g_recVenInvNo.NEXT = 0;

How can I do to generate the rest of the documents.

Thanks

Comments

  • bbrownbbrown Member Posts: 3,268
    Create a codeunit that accepts a single record (form your table) as a parameter. This codeunit would create one PO.

    IF g_recVenInvNo.FINDSET(TRUE,FALSE) THEN
      REPEAT
        IF f_CreateOrder(g_recVenInvNo) THEN BEGIN
          g_recVenInvNo.DELETE
        END ELSE BEGIN
          g_recVenInvNo."Has Error" := TRUE;
          // you could use GetLastError to save actual error message
          g_recVenInvNo.MODIFY
        END;
        COMMIT;
    UNTIL g_recVenInvNo.NEXT = 0;      
    
    
    
    There are no bugs - only undocumented features.
  • tobarichtobarich Member Posts: 33
    I´m working on Nav 4.0 and when I try to do:
    IF f_CreateOrder(g_recVenInvNo) THEN BEGIN
    

    my execution program finish because function f_CreateOrder fails. I can not continue asking.
  • bbrownbbrown Member Posts: 3,268
    did you update you codeunit to accept the record as a parameter?
    There are no bugs - only undocumented features.
  • kinekine Member Posts: 12,562
    As bbrown tried to describe - you need to use construction:
    if mycodeunit.RUN(MyRecord) then ... else...
    

    It is not working when calling another function in the codeunit. The OnRun trigger of the codeunit must be used. In this case, when there is some error during the execution, the "else" branch of the if-then will be called and you can do it in a loop for every record you want. If you are in NAV 5.00 you can use the GetLastError command to read the error text and you can e.g. store it in your source table...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    It's the same principle like in Report 297

    Regards
    Do you make it right, it works too!
Sign In or Register to comment.