Monday, March 25, 2013

AX Tables: Copy all table data to another table

Sometimes there is a need to copy the data that is in one table into a new table. This can be due to synchronization issues where the data in a table may be lost, just wanting to tinker with the data, or whatever other reason you want. Here is a basic way to do this.
  1. In the development workspace, right click on the table (we will call this daxTable) and select 'Duplicate'. This will create a new table. We will rename this table daxTable_Copy
  2. Copy, paste, and run the below code into an AX job
Note that the recId and recVersion will not come over in this call. If other tables rely on this table's recId, this will screw some stuff up moving the data around.

// Copy data from one dropped table to another
static void daxCopyTableData(Args _args)
{
    DAXTable      daxTable;
    DAXTable_Copy daxTable_Copy;
    ;

    ttsBegin;
    // Clear the data from the copy table
    delete_from daxTable_Copy;

    while select daxTable
    {
        buf2Buf(daxTable, daxTable_Copy);   
        daxTable_Copy.insert();
    }
   
    ttsCommit;
}

12 comments:

  1. Replies
    1. You're very welcome! Thanks for visiting and leaving a comment that indicates it was helpful! I really do appreciate the feedback and knowing that these posts help people. It's why I do it :-)

      Delete
  2. but how to copy the content of the particular field

    ReplyDelete
    Replies
    1. You would do a general assignment of field to field from the two tables. such as 'daxTable_Copy.Field1 = daxTable.Field1'. After the copy, you can manipulate whatever fields you want. This example was strictly for a 1:1 identical copy process.

      Delete
  3. Hi Justin,
    Doubt on From!
    How to copy the selected record from one form to another form.
    Thanks In Advance!!!

    ReplyDelete
    Replies
    1. Same Question from My Side Too...??

      Delete
    2. How to copy a single record from one form to another form? You would want to select the record (or multiple) on the source grid. Then I would write the data selected to the table that is being displayed on the other form. Assuming the form is open, you should just be able to refresh the destination form grid and the data will show up. May not be as cut and dry in your scenario but it should work.

      The grids in forms are basically just there to display data that exists in the backend. So you'll need to write the data in the backend so the data will show up in the front end.

      Did that help? Did I miss the question?

      Delete
  4. Getting this Error while runing the job--> " Error executing code: The field with ID '60009' does not exist in table 'daxTable_Copy' "

    ReplyDelete
    Replies
    1. Even I am getting the same error while running the job.Did u findout any solution for this problem.

      Delete
    2. Just verify your field Id in the properties of a certain field in both original and duplicate table.if all the field Id's are equal.it will copy the data,if not recreate the table and try it.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete