Can't use the answer from a Code Unit through a Web Service

HenrikDKHenrikDK Member Posts: 14
Hey,

I am making a Time Registration Solution, but I receive an error when I try to use it through a web service. The code is placed in the insert/modify trigger and is run to check if the line can be inserted in a Job Journal Line. The actual code that inserts the line is placed in a Code Unit, so on-screen error messages can be avoided.

The error I reseive:
Codeunit.Run is allowed in write transactions only if the return value is not used. For example, 'OK := Codeunit.Run()' is not allowed.

The code lines in the table:
IF NOT(CODEUNIT.RUN(CODEUNIT::"TR Test Insert Line",pTimeRegistration)) THEN BEGIN
  pErrText := lText003;
  EXIT(pErrText<> '');
END;

The code that tries to validate and insert the line in the Job Journal Line, is in the TR Test Insert Line-Code Unit.

Do you have an idea how to solve it or a possible work around?

Hope you can help.

Comments

  • krikikriki Member, Moderator Posts: 9,110
    The error is quite clear, I would say.

    You are in a transaction and want to do a "IF CODEUNIT.RUN(..) THEN" which is not allowed.

    Solutions are 2:
    -Or you do a "CODEUNIT.RUN(..);" without the IF THEN.
    -Or you make sure you are NOT in a transaction. This last you can do by putting a COMMIT just before the "IF CODEUNIT.RUN". BUT BEWARE: I don't know what you have done before that statement so you are in a transaction. By putting a COMMIT there, you can endanger integrity of your data in case something goes wrong after THE COMMIT but before the end of the complete job.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • HenrikDKHenrikDK Member Posts: 14
    Thanks for the answer.

    I need an answer from the Code Unit, because I am trying out if I can post a line in the Job Jounal Line table.

    The reason why I moved it to a Code Unit, is because I get an onscreen error and stop of code-run, when I do it inside the table it self.

    Do you have an idea how I can check if the fields in Job Journal Line can be validated and afterwards inserted; other then using Commit?

    /Henrik
  • SogSog Member Posts: 1,023
    use a temp table job so the validation is an option.
    Then after everything is good transfer the records to the real table and post :)
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • HenrikDKHenrikDK Member Posts: 14
    The problem will be the same in a temp table; I am trying to execute some code that validates values into fields and trying to insert records. To avoid error msg I have put this code in a code unit that is run with IF NOT(CODEUNIT.RUN()) Then and this works great inside NAV. When I try to do this via the web service, then it fails and gives me the error msg. posted in the original post.

    Any ideas? Thanks in advance.

    /Henrik
  • krikikriki Member, Moderator Posts: 9,110
    The only thing you can do is redesign all, so that you are NOT in a transaction when you do a "IF CODEUNIT.RUN(...) THEN".
    I am afraid there is no other option.

    While I was writing the last phrase, I found another option:
    Instead of calling the codeunit like that, do another webservice call from NAV just to try your test. Maybe this works.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • HenrikDKHenrikDK Member Posts: 14
    And what is the definition of "a transaction"?

    Sorry if it is a stupid question; just need the terminology to be correct :)

    /Henrik
  • krikikriki Member, Moderator Posts: 9,110
    A transaction s a block of changes (Insert/modify/delete) to the DB that must be committed as a whole or not at all.
    NAV starts a transaction automatically at the first change to the DB until a COMMIT is run or until the control is given back to the user.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.