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.