Getting Update operations are not allowed across companies message

I got this error recently: "Cannot edit a record in CBTestTable (CBTestTable). Update operations are not allowed across companies. Please use the changecompany keyword to change the current company before updating the record.". Of course, we all know what it means and how to fix it.

For those who don't, it means that you are trying to update a record for company A while being with the execution context in company B. Normally, this would not be the case, but as I mentioned I received it in a certain scenario and if you do get this as well, you might be in this case as well, so verify.

The issue in my case was not the lack of using the changeCompany keyword, but rather calling this twice. The business logic required to perform updates on several tables in multiple companies, so I was calling the changeCompany in sequence. Receiving the error, you would notice that the table buffer you are using will not change its DataAreaId when selecting a record in the second company's context. Note, this would be a table buffer variable you are using at a scope which does not resets it.

So, the simple fix: perform a tableBuffer.clear() in the subsequent changeCompany code block.

No comments:

Post a Comment