Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CODEUNIT.RUN() can't always be used

1,223 views
Skip to first unread message

Greg

unread,
Apr 30, 2009, 3:04:43 PM4/30/09
to
Hello,

I came across a situation where I needed to use CODEUNIT.Run(), but I
receive an error indicating I cannot use it in this situation. It mentions
something about not being able to use the return value from CODEUNIT.Run()
within a write transaction. However, I need to use the return value to do
what I am trying to achieve, so I feel somewhat stuck. The error also
mentions that I could move my code outside the write transaction, but that's
not feasible for a few other various reasons.

Let me explain exactly what I'm trying to do, to see if there are any other
options I am missing.

I am attempting to call into a COM DLL (Automation variable), but the call
can throw an exception if the URL is invalid. I want to catch this error,
but if I simply call this method directly, I get a message saying something
along the lines of "This message is for C/AL programmers. The call to xxx
failed ..." I discovered I could override this by placing the automation
method call into the OnRun method of a codeunit and call CODEUNIT.Run(),
using the return value to determine whether it failed or not. By doing that,
I could override that error message and display one that is slightly more
user friendly. However, as noted earlier, I cannot always do this. Is there
any way to override the error/exception returned by the automation call?

Any help is greatly appreciated.

Thanks,
Greg

Daniel Rimmelzwaan

unread,
Apr 30, 2009, 5:48:24 PM4/30/09
to
Put a COMMIT statement right before the RUN call. Be aware that this commits
the transaction up to that point, so make sure you don't commit half a
transaction.

"Greg" <Gr...@discussions.microsoft.com> wrote in message
news:ACA2B3A3-5CFA-4B15...@microsoft.com...

Greg

unread,
May 1, 2009, 9:38:10 AM5/1/09
to
Thanks for the quick reply.

The only thing I fear about doing this is that it will commit things that
shouldn't be committed. The place where I am attempting to do this is nested
within some stock NAV code (in several places), and so I fear it would affect
the original NAV code in a harmful way. In particular, this code is called
during tax calculation, which is in turn called from OnAfterGetRecord() on
the Sales Order Stats. form, or when a document is released, or, most
importantly, when a document is posted. A COMMIT halfway through posting,
which is when we calculate tax, would likely be disasterous. Is there any
other workaround to this?

Thanks,
Greg

Daniel Rimmelzwaan

unread,
May 1, 2009, 12:30:15 PM5/1/09
to
You're right, you don't want to commit incomplete transactions. Other than
rearranging your code to have complete transactions around the codeunit call
(maybe put the entire transaction into a codeunit, and manage the looping
from your single instance codeunit, just brainstorming here :)), I'm afraid
I don't know any other solution, it's just the way that it works. The
annoying thing is though that all of a sudden it might be accepted, and I
don't really understand what the underlying rules are. I've had instances
where it worked, then it stopped working, and then later it worked again.
Trial and error from here on out, and trust me, I know how frustrating this
is.

"Greg" <Gr...@discussions.microsoft.com> wrote in message

news:40908E9F-54E2-4EB4...@microsoft.com...

Robert

unread,
Jun 6, 2009, 2:31:01 AM6/6/09
to
You need to make another DLL that wraps the COM DLL you are calling have it
trap the error and convert it into an error return or event.

VB6 used to be very good for this; DotNET not so much, as there's going to
be a large performance impact especially if you're not calling it often.

Robert.

0 new messages