Dynamics 365 for Finance and Operations contains numerous temporary tables that are used by the application. We also have the option of creating Temporary tables based on any of the existing table in the AOT. They can be created in order to hold temporary data for the duration of a code block or snippet. As an example, we will use the customer to insert and display a couple of temporary records without affecting the actual data.
1 As an example, we will use the customer table to insert and display a couple of temporary
records without affecting the actual data stored in Dynamics 365. Lets add a new class to our Visual Studio Project. Right Click project name
2 Click Add a new item, we are going to add a new class for our temporary table
3 Click New Item. In the pop up window which is displayed I am going to select code in the left hand window. This will show me all the code options I can add
4 select runnable class in the code pane options and give the class a name. I am going to call the class RunnableClassCustomerTempTable
5 Right Click our new class object RunnableClassCustTempTable. Click in the menu option which is displayed Set as Startup Object. This will run this code when we start to run the project
6 In the code pane of the class enter the following lines of code. The key method in this recipe is setTmp(). This method is available in all the tables, and it makes the current table instance behave as a temporary table in the current scope.
7 The code creates an InMemory temporary table that has the same schema as the original table. In the code, we first call the setTmp() method on the custTable table to make it temporary in the scope of the code. The table data will not be affected.
8 Next let's run the code and see what the result is. You can debug or start without debugging depending on how you want to review the code running
9 Once the code is running. A browser window pops up with your code running in it. In this example we can see that there are two messages displayed in the list. Click the number 2 in the pop up window.
10 You can see the two customer number records which you have created and displayed.That's it. You're done. You have created a temp table and inserted two records into it. If you want check the Customer table to validate no new records were inserted.
X++ Code
class RunnableClassCustTempTable
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
CustTable customerTable;
customerTable.setTmp();
customerTable.AccountNum = '1000';
customerTable.Blocked = CustVendorBlocked::No;
customerTable.Party = 1;
customerTable.doInsert();
customerTable.clear();
customerTable.AccountNum = '1002';
customerTable.Blocked = CustVendorBlocked::All;
customerTable.Party = 2;
customerTable.doInsert();
while select customerTable
{
info(strFmt("%1 - %2",customerTable.AccountNum,customerTable.Blocked));
}
}
}