Basics of changecompany

The command changecompany is used to switch between companies in you Dynamics Ax environment through X++ code. This is the basics:

  // We are now in company FO
  changecompany("FO1")
  {
      // We are now in company FO1.
  }
  // Back in company FO

Let’s say that we start out in company FO and want to copy some data from company FO to company FO1. This is one way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 static void FO_changecompany(Args _args)
 {
    FO_TestTable    table, table2;
    ;
 
    while select table
    {
        changecompany("FO1")
        {
            table2 = null;
 
            table2.ttsbegin();
            table2.Id           = table.Id;
            table2.Name         = table.Name;
            table2.coolOrNot    = table.coolOrNot;
            table2.insert();
            table2.ttscommit();
        }
    }
 }

When trying to figure this out I first used this command to set data from table to table2:

 table2.data(table.data());

This didn’t work at all, all you got was two new records in company FO. My guess is that this was because all data including dataAreaId was copied and inserted when using table2.data(). This means that it doesn’t matter which company you are in when doing the insert, because dataAreaId always is set to FO when copying records from FO this way.

If you set the fields induvidually, dataAreaId is left blank and is only set when the insertion of data is done to the database. When this is done, the value that is assigned is based on what company you are currently in, and in our job FO_changecompany this means FO1.

Last 5 posts in Development

Leave a Reply

Your email address will not be published. Required fields are marked *