AX – D365FO – Copy records from one grid to a temporary grid (with setTmp() method)

We want to copy records from a grid that contains all records of a table to another that contains same type of records but only the one we selected from the first one

This is what we want to obtain.

First we must create a Form, then add two data source that points to the same table (for example LogisticsAddressState)

Both points to LogisticsAddressState table.
The first must be called “LogisticsAddressState” and the second “LogisticsAddressStateTmp”

Then create 2 grids.

The first points to the first data source and the second to the second data source

Then create 2 command buttons

The “AddState” button is an Add command, the “RemoveState” button is a Remove command

Then we start to write code

In the init method of the form we set the LogisticsAddressStateTmp data source as tmp datasource

    public void init()
    {
        super();
        LogisticsAddressStateTmp.setTmp();
    }

Then we create a method to insert data from first grid to the second one

   void addSelectedStates()
    {
        LogisticsAddressState               logisticsAddressStateTmpLocal;
        LogisticsAddressState               logisticsAddressStateLoop;
        MultiSelectionHelper helper = MultiSelectionHelper::construct();

        logisticsAddressStateTmpLocal.setTmp();

        helper.parmDatasource(logisticsAddressState_ds);
        
        logisticsAddressStateLoop = helper.getFirst();

        //Loop through selected records of the grid
        while (logisticsAddressStateLoop)
        {
            // insert State into selected States table
            logisticsAddressStateTmpLocal.setTmpData(LogisticsAddressStateTmp);
            
            //Check that State is not already in selected,
            select firstonly RecId from logisticsAddressStateTmpLocal
                where logisticsAddressStateTmpLocal.StateId == logisticsAddressStateLoop.StateId;//logisticsAddressStateSelected.RecId;
            if (logisticsAddressStateTmpLocal.RecId == 0)
            {
                logisticsAddressStateTmpLocal.StateId = logisticsAddressStateLoop.StateId;
                logisticsAddressStateTmpLocal.Name = logisticsAddressStateLoop.Name;
                logisticsAddressStateTmpLocal.insert();
            }
            
            logisticsAddressStateLoop = helper.getNext();
        }

        // update selected records grid
        logisticsAddressStateTmp_ds.executeQuery();
    }

After that we override the clicked() method of the AddState button to call the addSelectedStates() method

    [Control("CommandButton")]
    class AddState
    {
        /// <summary>
        ///
        /// </summary>
        public void clicked()
        {
            element.addSelectedStates();
            super();
        }

    }

Then we create a method to remove data from the second grid

    public void removeSelectedStates()
    {
        LogisticsAddressState               logisticsAddressStateTmpDelete;
        LogisticsAddressState               logisticsAddressStateLoop;
        MultiSelectionHelper helper = MultiSelectionHelper::construct();

        logisticsAddressStateTmpDelete.setTmp();
        logisticsAddressStateTmpDelete.setTmpData(LogisticsAddressStateTmp);

        helper.parmDatasource(logisticsAddressStateTmp_ds);
        logisticsAddressStateLoop = helper.getFirst();

        ttsbegin;
        //selectionCriterion = getFirstSelection(vendTmpReviewCriterion_ds);
        while (logisticsAddressStateLoop)
        {
            delete_from logisticsAddressStateTmpDelete
                    where logisticsAddressStateTmpDelete.StateId == logisticsAddressStateLoop.StateId;
            
            logisticsAddressStateLoop = helper.getNext();
        }
        ttscommit;

        // update selected grid
        logisticsAddressStateTmp_ds.executeQuery();
    }

After that we override the clicked() method of the RemoveState button to call the removeSelectedStates() method

    [Control("CommandButton")]
    class RemoveState
    {
        /// <summary>
        ///
        /// </summary>
        public void clicked()
        {
            element.removeSelectedStates();
            super();
        }

    }

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