I'm writing a routine that will post sales order shipments in code, I have it working, however, if there are any errors in the posting of the order, my program terminates.
I would like to trap errors, however, C/AL does not allow me to put conditions to check (IF codeunit90.run(SalesHdrRec) etc..) if the posting routine completes successfully.
Can someone tell me how I can do this so that I can trap any errors and not allow my routine to terminate? Thanks much.....
A.L.
0
Comments
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
Whenever I need to run a routine and catch the errors I use a 50000 codeunit that has this code on the OnRun() trigger:
I can call this codeunit from any other object, wrting code like this one:
I use the MyErrorTxt variable to write the error in a log file, send an e-mail, or any other log system you may use.
Salut!
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
Thanks for your responses, maybe I did not explain my issue correctly, I'll make another attempt at this.
Below as my code:
SalesHdr.Ship := TRUE;
SalesHdr.Invoice := FALSE;
SalesHdr.VALIDATE("Posting Date",TODAY);
IF SalesPost.RUN(SalesHdr) THEN BEGIN
inEDIHeader."Document Posted" := TRUE;
inEDIHeader."Doc. Posted Date Time" := CURRENTDATETIME;
inEDIHeader.MODIFY;
COMMIT;
END
ELSE
...... display error (or something else)... and continue executing this routine....
Where SalesHdr is the Sales Order table and Sales Post is the Sales-Post routine (std. codeunit 80).
If I try to execute the above code, I get the below error message. From what I understand it does not like the conditional run check (IF) when running a codeunit when there are write transactions in that codeunit where tables will be locked.
Can someone tell me how I can write the code in such a way that will allow me to do what I need to do, that is, to know and trap any errors and not have the code terminate? I'm using NAV 2009 Classic. Thanks again.
error i get.....
The following C/AL functions can be used only to a limited degree during write transactions because one or more tables will be locked.
Form.RunModal is not allowed in write transactions.
CodeUnit.Run is allowed in write transactions only if the return value is not used. For example, 'OK := CodeUnit.Run()' is not allowed.
Report.RunModal is allowed in write transactions only if 'RequestForm = FALSE'. For example, 'Report.RunModal(...,FALSE)' is allowed.
DataPort.RunModal is allowed in write transactions only if 'RequestForm = FALSE'. For example, 'DataPort.RunModal(...,FALSE)' is allowed.
Use the COMMIT function to save the changes before this call, or structure the code differently.
OK
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
That's what the problem was, thanks all for you help.
A.L.
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.