D365F&O, Developer

How to insert new data in D365 F&O

One of the things you will usually do in D365F&O is to work with data. In this article, we will learn more about different ways to insert new data in D365F&O using the x++ programming language.

Before starting writing the code we need to find where to put our code. Usually, this depends on the business logic that we are following. In our case, we will create a Job as a test class to demonstrate how to insert new data in D365. As we said there are different ways that we can insert new data in D365F&O.

Using Insert Method.

Usually, when we are creating a new record in D365 we use the insert method. It’s an easy way to do it and when we want to insert only one record is the most efficient way. First, we need to declare a table buffer and after that, we should specify the values of all fields that are unique indexes or mandatory fields in the table. Optionally based on business logic we can specify the values of other fields that aren’t unique indexes. The next and last step is to call the insert method on the table buffer. The code should be written inside transaction blocks (TTSBegin and TTSCommit). Below I will attach the example code to insert a new record in WHSContainerTable using the ‘Insert’ method.

class HowToInsertData
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        WHSContainerTable containerTable;
        
        ttsbegin;
        containerTable.ContainerId = 'CON-000000000';
        containerTable.ShipmentId = 'SHP-0000001';
        containerTable.insert();
        ttscommit;
    }

}

How to run Job/Runnable class.

To run an object in D365F&O you need to set it as a startup object. Right-click on the class that we created and select ‘Set as Startup Project’ and pres Start in Visual Studio. After the running process of the class has finished we can check if the record is inserted in the table.

Table Browser

If we want to view the table records we have to open the table on table browser. Usually I open it through Visual studio.

  1. Find the table in AOT.
  2. Right click on the table.
  3. Select ‘Open table browser’.

Now I can see all the records of the table in the browser.

Using doInsert Method.

doinsert method is similar to the Insert method. The only difference is that when we call the doinsert method, the code overridden in the ‘Insert’ method will not be executed. For this reason, this method is not usually used. In my experience, I have used this method only when I want to skip all the logic behind the insert method (usually skipping validations). Below I will attach the example code to insert a new record in WHSContainerTable using ‘doinsert’ method.

class HowToInsertData
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        WHSContainerTable containerTable;
        
        ttsbegin;
        containerTable.ContainerId = 'CON-000000000';
        containerTable.ShipmentId = 'SHP-0000001';
        containerTable.doInsert();
        ttscommit;
    }

}

Using Insert_RecordSet.

The insert_recordset is used usually when we want to copy multiple existing records from one table to another. It’s very fast way because it generates a single SQL statement that is sent to the server. I will attach the example code to insert a new record in WHSContainerTable using ‘insert_recordset’ method.

class HowToInsertData
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        WHSContainerTable containerTable;
        WHSContainerTableStagging containerTableStagging;

        insert_recordset containerTableStagging (ShipmentId, ContainerId)
            select ShipmentId , ContainerId 
                from containerTable 
                where containerTableStagging.ContainerId != ''
                && containerTableStagging.ShipmentId != '';
    }

}

In our example, we have created a WHSContainerTableStagging where we have all containers imported from an external application. And we want all of these containers where containerId and shipmentId is specified to add in the standard table (WHSContainerTable)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s