Skip to main content

MSDYN365BC - Be Ready for Programming without With Statement?

Hi Readers,

Due to some issues with "with" statement in SaaS Environment Microsoft have to decided to remove with Statement going forward.

So its time to say good bye to With Statement.

In this article we will discuss what that means to developer and what we will have to change. 

I don't want to give a timeline but I hope that it should get deprecated sooner than later.
I think there are two different sets of developers one who always liked with statement in Dynamics NAV or Business Central and another set of people who hate with statement and never use it while coding.

I am one of those who liked with statement as i always thought it make code more readable.

But either you loved/used it or not this change will impact you.

First lets understand what is the warning with statement - 

"The 'with' statement is deprecated and will be removed for cloud development in a future release. This warning will become an error in a future release."

I don't see that Warning Message?

You will not see this warning with Business Central 2020 Wave 1. 
You can only see this warning message in your extension if you are using Insider build. 

Which Extension will have impact of this change?
  • Extensions which are available in App Source.
  • Per Tenant Extension (PTE) which you have developed for your BC SaaS Customers.
I will also include on-prem Extensions. Please start doing these changes to that you don't have to rework when customer decides to move to SaaS.

So lets see what is the problem.

There are two types of With Statement in Dynamics NAV / Business Central -
  • Explicit With Statement.
  • Implicit With Statement.
What is Explicit With Statement?

When we as developer write a With Statement in our custom code in Extension those code segment are Explicit With Statement. A Simple example is in screenshot below.



What is Implicit With Statement?

When we as developer write an With Statement, but its added by compiler automatically that is called as Implicit With Statement.

Examples - OnRun Trigger in Page / Codeunit and Fields in Page.

Screenshot below show an example of Implicit With Statement.

Codeunit Example - 


Page Example - 



What is the Issue with "With statement" ?

Either Implicit or Explicit here is the challenge that Microsoft is facing on SaaS. Suppose you have a codeunit which Scans all vendor and calls a function ValidateVendorPurchaseCode which is a local function in same codeunit.

But at the same time there is a possibility that -
  • Microsoft is using same function Name in Base App.
  • One or more Addon partners is using same function name in Table Extension. 
As the function is written within the Scope of With Statement, System will point to a different function that you want to call.

As a developer you will not be able to understand the issue because its not a compiler error till the parameters are same. And its impossible to execute the correct code.

A Sample Example is shown below. 



So we understand the Issue. What we will have to do?

All Implicit / Explicit With statement need to be changed. 

Implicit With - 
  • For a New Page / Page Extension in Your Extension Make sure that all Fields defined in Extension Should be changed from - 
    • field(Name; Name) To
    • field(Name; Rec.Name)
  • For a Codeunit/Page On Run.
    • Create a function and pass the Rec. OR
    • Change all code in OnRun with Rec.
Explicit With - 
  • For All Explicit With all need to rewritten to include the Table Variable Name (.).

That Seems a Lot of Work. 

Yes that a lot of work if you have multiple extension but think about Microsoft who have to make these changes in whole base app.

Any Help from Microsoft?

Yes, Microsoft is introducing some quick fixes to change / update code.
  • A new Light bulb will be available which can do code change automatically.
  • If you see a with statement you can click on light bulb and code will be corrected / updated.
  • When you create a new page it will automatically bring the right syntax without implicit with.


At the end,

Change is good. Don't resist any change. This change is better for our future. 

As i said earlier I used and loved With Statements but if it causes problems to future I am ready to my bit to fix it and so do everyone should.
 
Let me know your views / questions as comment to this article.

Regards,
Saurav Dhyani

Comments

Popular posts from this blog

VIEW SERVER STATE permission on SQL Server?

Hi all, Sometime While trying to Login into a database we face an error message as shown below. --------------------------- Microsoft Dynamics NAV Classic --------------------------- You cannot start Microsoft Dynamics NAV Classic because you do not have the VIEW SERVER STATE permission on SQL Server. Contact your system administrator. --------------------------- OK    ---------------------------

NAV 2013 R2 - Cumulative update 12 Released.

Hi all, Please find below the details of  Cumulative Update 12 released for Microsoft Dynamics NAV 2013 R2. Title - Cumulative Update 12 for Microsoft Dynamics NAV 2013 R2 Build No. - 38053 Release Date - October, 2014 Local Version Included - AU, AT, BE, CH, DE, DK, ES, FI, FR, IS, IT, NA, NL, NO, NZ, SE, UK, RU Download Link Note: Implementing this cumulative update will require a database conversion unless you have already implemented update rollup 5.

NAV 2013 Upgrade Part III - Data Upgrading.

Hi all, As per agenda we need to discuss the data upgrade in NAV 2013. The process is similar to what used to be till NAV 2009 R2 but with some changes. Let's start the process. Remember -  1) You can only upgrade a database to NAV 2013 from NAV 2009 SP1 / R2 SQL Only. 2) The Synchronization of users with SQL Server is no more required.