Execute a job on data in several companies – part2

April 30, 2010 1 comment

In one of my previous posts I explained that you can switch company with the keyword changecompany. I also made a remark that you should set the value of your table-value to null after each changecompany.

Small code example (bad):

1
2
3
4
5
6
7
8
9
10
11
12
DataArea dataArea;
PurchTable purchTable;
;
while select dataArea
where !dataArea.isVirtual
{
    changecompany(dataArea.id)
    {
        select firstonly purchTable;
        info(strfmt("%1 (%2)", purchTable.purchId, purchTable.dataAreaId));
    }
}

The result is:

You can clearly see that the table variable is is known in the company you declared it.

Small code example (good):

1
2
3
4
5
6
7
8
9
10
11
12
13
DataArea dataArea;
PurchTable purchTable;
;
while select dataArea
where !dataArea.isVirtual
{
    changecompany(dataArea.id)
    {
        purchTable = null;
        select firstonly purchTable;
        info(strfmt("%1 (%2)", purchTable.purchId, purchTable.dataAreaId));
    }
}

The result:

So by making this small code change it runs perfectly.

NOTE: When you are calling a new method in the changecompany statement, you don’t have to set all the common variables that you declare  equal to ‘null’. This because they are declared after the companychange.

Crosscompany

April 30, 2010 No comment

Yesterday I talked about executing code/getting records in several companies. Now the changecompany is not the only method. You can also get data out of several companies by using the keyword crosscompany in your select statement.

Example:

1
2
3
4
5
6
7
8
9
Address     address;
container   conCompanies = [ 'cee', 'dat' ];
;
while select
crossCompany :conCompanies address
order by dataAreaId
{
    //Do something
}

Execute a job on data in several companies

April 29, 2010 2 comments

There is a verry simple trick to loop through some companies and execute some code in them.

Just write:

DataArea dataArea;
;
while select dataArea
where !dataArea.isVirtual
{
    changecompany(dataArea.id)
    {
        // Do Something...
    }
}

Note! Make sure that you clear the table variable after each companychange (common = null;).