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

BC 21 and Higher - PowerShell Cmdlet (Replacement of Business Central Administration).

Hi Readers, As discussed in last article about deprecating of Business Central Administration, there are few common actions that we use in administration till Business Central 20. For our on-prem customers, we will still require doing activities. As Microsoft suggest we need to start using PowerShell cmdlet.    Let's see how to do those via PowerShell, or Administration Shell. I will be keep adding commands as you comment to this article.

Send Mail with Attachment From Navision.

Hi all, We have seen how to save a report into PDF and how to send mail to a customer. Let's link these two post in one i.e. Mailing statement to a customer into PDF Format. This article is part of the Series. Please Refer  Table of Content here . If you have the old objects set let me brief you what I will be changing - 

MSDYN365BC - Data Upgrade To Microsoft Dynamics 365 Business Central on premises.

Hi Readers, We have already talked about the number of steps for upgrading to Business Central on Premises from different NAV versions. After that article, I received multiple requests for an article which list down steps for Data Migration. In this article, we will discuss steps of data migration to MSDYN365BC (on-Prem) from NAV 2017. For this article, I am considering a Cronus Demo Database without any customization. For an actual upgrade project, we will have to complete object merge using compare and Merge process. After the Merge Process, the next step is data migration. Let's discuss those steps. Direct Upgrade to Microsoft Dynamics 365 Business Central (on-Prem) is from following versions - 1. NAV 2015. 2. NAV 2016. 3. NAV 2017. 4. NAV 2018.