Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Our blog provides a curated collection of tips, tricks, and code snippets that streamline your workflow and enhance your proficiency.
Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

How to Use Normal Table as Temp Table in Dynamics 365 Finance and Operations

0

Microsoft D365 operates with normal databse tables and temporary table based on the requirement in a project, Regular tables are normal standard physical tables in SQL server database. Where as InMemory tables are temporary tables which are held in the memory and written to local disc after reaching a certain limit of the system especially in Application Object Server (AOS).

If you are a beginner or new to dynamics 365 Finance & Operations, you can use Microsoft'f Free Virtual Machine. Learn How to Set Up a Free Virtual Machine for Dynamics 365 Development

How to Use Normal Table as Temp Table in Dynamics 365 Finance and Operations

Why we Use Normal Table as Temp Table in Dynamics 365 Finance and Operations ?

Basically, Standared Dynamics 365 Finance and Operation projects contains many temporary table and regular tables.Imagine a scenario that you have to create a complex temporary table which is similiar to already existing regular table. In this situation you can use the existing regular table as temp table. As we already read, InMemory tables are temporary tables which are held in the memory and written to local disc after reaching a certain limit of the system especially in Application Object Server (AOS). So this method saves records local disc/memory instead of saving into database. Also deleting records from temporary table will not delete anything from database.

In Dynamics 365 Finance and Operation projects you can use the following coding snippets to convert a regular table to temp table.

As an example, we will use the vendor table to insert and display a couple of temporary records without affecting actual data.Yo create a Runnable Class named VendTableToTemp 

class VendTableToTemp
{
   
  public static void main(Args _args)
 {
      VendTable vendTable;
      vendTable.setTmp();
      vendTable.AccountNum = '1001';
      vendTable.Blocked = CustVendorBlocked::No;
      vendTable.Party =Supplier 1;
      vendTable.doInsert();
      vendTable.clear();
      vendTable.AccountNum = '1002';
      vendTable.Blocked = CustVendorBlocked::All;
      vendTable.Party = Supplier 2;
      vendTable.doInsert();
      while select vendTable
     {
        info(strFmt( "%1 - %2", vendTable.AccountNum, vendTable.Name));
      }
 }
 
}
Output
1002 - Supplier 2
1001  - Supplier 1

In the code, we first call the setTmp() method on the vendTable table to make it temporary in the scope of this method. This means that any data manipulations will be lost once the execution of this method is over and the actual table content will not be affected.

The key method used here to convert a regular table to temp table is setTmp(); 
which creates an InMemory Table which has the same structure as that of the original table. setTmp() : has nothing to do with deletion at all. It turns a buffer to a temporary one, therefore it saves records to memory/on disk instead of into database. If you delete everything from the temporary buffer, it doesn’t delete anything in database, obviously.

Next we will use doInsert() to bypass any validation rules which are not required for temporary table.

We have to keep in mind that even the table becomes temporary; all the code in its insert(), update(), delete(), initValue(), and other methods is still present and we have to make sure that we don't call it unintentionally.

If you like this article please share to your friends and your comment will be very valuable for improving the quality of the articles. So happy coding with D365 Snippets 🙂.

Post a Comment

0Comments
Post a Comment (0)