microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
728 stars 241 forks source link

[Bug]: TryFunction Error #7607

Open wgross92 opened 9 months ago

wgross92 commented 9 months ago

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;

}
RadoArvay commented 9 months ago

There are 2 points in the documentation Handling Errors using Try Methods:

So, probably, that is the reason of your issue (a combination of those points).

wgross92 commented 9 months ago

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# :)