[Bug]: TryFunction Error
Describe the issue
Situation: The result of procedure A is entered directly as a parameter in procedure B. Procedure B is a TryFunction. Procedure A is not a TryFunction. Result of procedure B is handelt with 'if' or asign to a boolean. However, if A executes a DB operation, a server error occurs because A is a TryFunction. Only its return was taken as a parameter.
Steps to reproduce
page 50000 MyPage
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
actions
{
area(Processing)
{
action(TestTryFunction)
{
trigger OnAction()
var
name: text;
myresult: boolean;
begin
//This works ok.
MyTryFunction(InsertNewContact('xx100'));
//This also works
name := InsertNewContact('xx101');
if MyTryFunction(name) then;
//This Trow Error to Eventlog that InsertNewContact is a try function
if MyTryFunction(InsertNewContact('xx102')) then;
//This also throw an error
myresult := MyTryFunction(InsertNewContact('xx102'));
end;
}
}
}
local procedure InsertNewContact(Code: Code[20]): Text
var
Cont: Record Contact;
begin
Cont.Init();
Cont."No." := Code;
Cont.Insert();
Exit('Hello');
end;
[TryFunction]
local procedure MyTryFunction(name: Text)
begin
if (name.Contains('')) then;
end;
}
There are 2 points in the documentation Handling Errors using Try Methods:
- Because changes made to the database by a try method aren't rolled back, you shouldn't include database write transactions within a try method. By default, the Business Central Server configuration prevents you from doing this. If a try method contains a database write transaction, a runtime error occurs.
- If a try method call doesn't use the return value, the try method operates like an ordinary method, and errors are exposed as usual.
So, probably, that is the reason of your issue (a combination of those points).
But this phenomenon is not described in the documentation:
myTryFunction( //THIS IS A TRY FUNCTION which receives a parameter myNormalFunction()); //THIS IS A NORMAL FUNCTION with a result. e.g.: a text
If the TryFunction is treated as a TryFunction, then in this case the Normal NOT TryFunction is also treated as a TryFunction.
It is not called by the try function, only the result is passed directly to a try function.
It is ok if that is how it should be, but it is not documented. There is also no corresponding error message. The error message says that function "x" is a try function, but if the developer looks in the code this is not the case. Perhaps it has not been common practice under AL/NAV to use resutls directly. But it will be used more and more as it gets closer to "normal" languages like C# :)