getlasterrortext

Hi,

I have created two tables with integer fields in both the fields of different length, I also have a separate field in the 2nd table to save the error message received in the last transaction.

But what I want if I transferring a total of 100 records means and if the error occurs at the 10 record the transactions stop, I want the transaction to be continuous even if the runtime error occurs.

//TransferRecords.RUN;
totable.DELETEALL;
fromtable.RESET;
fromtable.FINDFIRST;
REPEAT
//totable.INIT;
//totable.TRANSFERFIELDS(fromtable);
//totable.INSERT;
//MESSAGE(FORMAT(‘code runs here’));
COMMIT;
IF NOT errorcapture.RUN(fromtable) THEN
BEGIN

totable.INIT;
totable.Field1 := ‘Prim_Key’;
totable.INSERT;
totable.ErrorText := COPYSTR(GETLASTERRORTEXT, 1, MAXSTRLEN(totable.ErrorText));
totable.INSERT;
END;

//totable.ErrorText := COPYSTR(GETLASTERRORTEXT,250);
UNTIL fromtable.NEXT =0;

moreover, I don’t want to give any hardcode values to the field which is generating the error.

errorcapture[codeunit]
totable.INIT;
totable.TRANSFERFIELDS(Rec);
totable.INSERT;

Please advice.

Regards,

Bharath K

you could do OK := table.INSERT and then catch error (OK will return TRUE of insert is successful)

Maybe use tryfunction type of function some where which has similar concept to try…catch c#

Can you explain in detail.

Take a look here:

https://docs.microsoft.com/en-us/dynamics-nav/handling-errors-by-using-try-functions

Besides in your code you do a fromtable.FINDFIRST; - THIS IS WRONG. NEVER use a FINDFIRST if you need more than one record. then use either FINDSET or FIND(’-’) (Preferable FINDSET if you are modifying records).

ALSO never use TABLE.INSERT / TABLE.MODIFY or TABLE.DELETE without adding (TRUE) meaining in your example: totable.INSERT(TRUE)

You can arrange code like this

IF YourCodeunit.RUN THEN
HandleSuccess
ELSE
HandleError

totable.DELETEALL;
fromtable.RESET;
fromtable.FINDFIRST;
REPEAT
//totable.INIT;
//totable.TRANSFERFIELDS(fromtable);
//totable.INSERT;
//MESSAGE(FORMAT(‘code runs here’));
COMMIT;
IF NOT errorcapture.RUN(fromtable) THEN
BEGIN

totable.INIT;
totable.Field1 := ‘Prim_Key’;
totable.INSERT;
totable.ErrorText := COPYSTR(GETLASTERRORTEXT, 1, MAXSTRLEN(totable.ErrorText));
totable.INSERT;
END ELSE BEGIN

totable.INIT;
totable.Field1 := ‘Prim_Key’;
totable.INSERT;
totable.ErrorText := Success;
totable.INSERT;

END;

//totable.ErrorText := COPYSTR(GETLASTERRORTEXT,250);
UNTIL fromtable.NEXT =0;