Table of Contents

Error Handling

Error handling for file operations is controlled by a set of functions, which we will cover here. In most cases, Business Central will stop executing code and roll back a transaction if the code fails. This is a bit more complex in file operations because part of the file operation is a commit to the database. The commit makes the file request visible to the local file service that handles the requests.

The file service codeunit has a SetErrorAction function. This function tells the file service what to do if an error occurs. Sometimes, you may want to continue your code execution and check for errors afterward. Other times, you may choose to raise an exception and stop the code if an error occurs.

procedure SetErrorAction(ErrorAction: enum "ForNAV File Error Action")

The code will continue to run if you call the SetErrorAction function with the Ignore value.

FileService.SetErrorAction("ForNAV File Error Action"::Ignore);

The error that happened is retrieved by calling the LastError function.

procedure LastError(): Text

If you are not running in batch mode, most file operation functions return true or false, indicating if an error occurred. In batch mode, the function returns an identifier. The task identifier can be used to get the error status for the individual task.

procedure GetTaskError(Id: Integer): Text

Setting the error action to Ignore will make the batch mode continue with other tasks if one of the tasks fails.