There is a bug with changeCompany keyword which prevents usage of the same initialized table buffer in different companies. For example, in the following code:
inventItemGroup.ItemGroupId = 'IA';
inventItemGroup = null;
inventItemGroup.ItemGroupId = 'IB';
Without inventItemGroup = null; line the second insert will throw an error:
Cannot create a record in Item groups (InventItemGroup).
Insert operations are not allowed across companies. Please use changecompany keyword to change the current company before inserting the record.
Because buffer still has dataAreaId value equals to 'A' and neither changeCompany nor insert does the substitution automatically.
So you need to either avoid using the same buffer in different companies or do not forget to reset it via assigning null value.