Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Issue with TryFunction in BC on-premise

(0) ShareShare
ReportReport
Posted on by 2,211 Moderator

Hi,

I'm facing an issue with TryFunction in BC on-premise. Unlike in BC online, it is not throwing an error during code execution.

Here's an example of my code:

procedure Test()
begin
    if (batchAllocation.FindSet()) then
        repeat begin
            if (DistributionProcess()) then begin
                ErrorInProcess := false;
            end
            else begin
                ErrorInProcess := true;
                RevertProcess();
            end;
        end;
        until batchAllocation.Next() = 0;
end;

[TryFunction]
procedure DistributionProcess()
begin
    // Perform various tasks
end;
The RunTime problem occurs when inserting records in the purchase header table

999.png

I would greatly appreciate any suggestions on how to resolve the issue in on-premise version.

Thank you in advance for your help

  • JJMc Profile Picture
    291 on at
    RE: Issue with TryFunction in BC on-premise

    Just out of curiosity. Could you please share how you solve it? Using a codeunit for this purpose?

    Thank you.

  • Govinda Kumar Profile Picture
    2,211 Moderator on at
    RE: Issue with TryFunction in BC on-premise

    Thanks, JJMc2.. I'm glad to inform you that the issue is resolved, and I have found an alternative way to achieve my desired functionality that works both in the on-premise and on-cloud, thank you for your response.

  • JJMc Profile Picture
    291 on at
    RE: Issue with TryFunction in BC on-premise

    Good morning.

    Try functions's behavior are diffrent in cloud and OnPrem, but you can change it:

    According to Microsoft:

    This behavior is different from the behavior in Microsoft Dynamics NAV 2016, which did not include this restriction by default. Therefore, you might encounter errors if you have application code that was written for Microsoft Dynamics NAV 2016 and you run the code in Microsoft Dynamics NAV 2017.

    In practice, this means that you should not include the following function calls inside a try function scope:

    Data Type

    Function

    Record and RecordRef

    - INSERT
    - MODIFY
    - MODIFYALL
    - RENAME
    - DELETE
    - DELETEALL
    - >ADDLINK
    - DELETELINK
    - DELETELINKS

    Database

    - COMMIT

    If you want to change this behavior, you can set the DisableWriteInsideTryFunctions setting in the CustomSettings.config file of the server instance to false. However, we recommend that you design your code to suit the default behavior instead.


    So you may find the CustomSettings.config file at: C:\Program Files\Microsoft Dynamics NAV\100\Service


    Once you find the file, open it (Make sure the service is stopped) and search for DisableWriteInsideTryFunctions in this file. Change it to false.



    Save and close the file and then start the service again. Now try the operation again and It must be fixed. Enjoy!!!

    Reference: docs.microsoft.com/.../handling-errors-by-using-try-functions

     Note

    This behavior is different from the behavior in Microsoft Dynamics NAV 2016, which did not include this restriction by default. Therefore, you might encounter errors if you have application code that was written for Microsoft Dynamics NAV 2016 and you run the code in Microsoft Dynamics NAV 2017.

    In practice, this means that you should not include the following function calls inside a try function scope:

    TABLE 1

    Data Type

    Function

    Record and RecordRef

    - INSERT
    - MODIFY
    - MODIFYALL
    - RENAME
    - DELETE
    - DELETEALL
    - >ADDLINK
    - DELETELINK
    - DELETELINKS

    Database

    - COMMIT

    If you want to change this behavior, you can set the DisableWriteInsideTryFunctions setting in the CustomSettings.config file of the server instance to false. However, we recommend that you design your code to suit the default behavior instead

    Discalimer: 

       

     

    I hope it is usefull.

  • Suggested answer
    Govinda Kumar Profile Picture
    2,211 Moderator on at
    RE: Issue with TryFunction in BC on-premise

    The problem has been resolved, and the issue mentioned above is explained in detail in this blog demiliani.com/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,978 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,821 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans