If Codeunit.run then

IBSIBS Member Posts: 29
Hello I have the following code.
if Item.findset repeat
  if MyCodeUnit.run(Item) then begin
     WriteLog( GETLASTERRORTEXT);
   end;
until Item.next = 0;



MyCodeUnit.run just updates the Item record.


If there is an error on last Item, will the whole batch role back or not?

I tested this and it does not role back. So where is the commit?

Comments

  • WaldoWaldo Member Posts: 3,412
    Indeed, your code is going to Log and Skip the records where the codeunit fails. By using IF Codeuint.Run THEN, you don't end the transaction.

    If you want to roll back, you might want to add ERROR('') (that also stops the loop, which is not happening with your code... .

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • garakgarak Member Posts: 3,263
    with an if XXX.run then begin ... end only the record with the "error" are not modified. all other are changed. so if you need a hole rollback over all records, write a error message in your "else" part of your if statement or use
    YourCodeunit.run(item);
    

    so, an error popps up an a rollback begins.

    Regards
    Do you make it right, it works too!
  • jlandeenjlandeen Member Posts: 524
    One thing about using the IF Codeunit.run THEN syntax is that it helps TRAP errors. The whole idea is that when you want to be able to have your code continue on without an error and without rolling back everything that is when you would use it.

    If you want the errors to propagate to the screen then just use codeunit.run without the If statement.
    IF MyCodeunitVariable.RUN(<input variable>) THEN BEGIN
      //Put code here to follow successful execution of run
    END ELSE BEGIN
      WriteLog( GETLASTERRORTEXT); 
    END;
    

    This works very well if you're executing code on a NAS and don't want errors to propagate.
    Jeff Landeen - Sr. Consultant
    Epimatic Corp.

    http://www.epimatic.com
  • ck1ck1 Member Posts: 6
    Can I use this Structure in case of Modify or Insert too?

    f.ex.
    IF NOT Table.Modify THEN BEGIN ... END;
    

    I want to handle Errors without stopping the whole process.
  • WaldoWaldo Member Posts: 3,412
    Yes you can... 8)

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • ck1ck1 Member Posts: 6
    thx!
    i tried in Objectversion 3.70 but the code i entered above journalised all positive entries (the "IF"-Case but not the "IF NOT"-Case).
    Do u know in which Version is this possible?

    Thx
  • WaldoWaldo Member Posts: 3,412
    hm, don't know what you mean, exactly.

    Do you mean the IF blabla.MODIFY works, but the IF NOT blable.MODIFY does not?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • ck1ck1 Member Posts: 6
    Yes!
    IF NOT Report.MODIFY THEN BEGIN
       Statement1              // for all Line with Errors in Modification
    END; 
    Statement2;
    
    Statement1 is executed for all Modified Lines(with no Errors) but not for the Error (= no Modification) Lines.
    Hope It's clear what i Mean :?
  • WaldoWaldo Member Posts: 3,412
    For modify, I don't know exactly, but what people use quite often is something like:
    IF NOT blabla.INSERT then blabla.MODIFY;
    

    Did you try:
    IF blabla.MODIFY THEN
      Statement1
    ELSE
      Statement2;
    

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • ck1ck1 Member Posts: 6
    i tested it over Night but i got the same (negative) effect.
Sign In or Register to comment.