Microsft Dynamics 365 Business Central allows having multiple companies in the same tenant. In this article, you will learn what is and how to use the ChangeCompany function. A useful tool to access data of all companies in the same environment via AL. Also, you will learn how to apply it with a real-life example.

  • What Is The ChangeCompany Function
  • Remarks To Be Aware Of
  • How To Use ChangeCompany
  • Use Case Example

What Is The ChangeCompany Function


The ChangeCompany function is used to access data from any company via AL. This function is useful for tenants with more than one company. Even more useful for tenants with several companies.

One of the most common ways to use ChangeCompany in Business Central is to gather data from different companies and present them in one place. This way, users can have a better experience because they won´t need to switch companies to see the data.

Remarks To Be Aware Of


ChangeCompany will take into account user rights. It won´t change the company if permissions haven´t been granted previously.

Triggers won´t work for the changed company. They will run in the current company. We will have a look at this restriction in the following section.

However, we can use the Task Scheduler to run processes, validations, and triggers in other companies. See how to use the Task Scheduler in the article below.

How To Use ChangeCompany


The way to use the ChangeCompany method is simple. We just need the name of the company that we want to change into. We can use Record or RecordRef.


Let´s see a simple example. This piece of code will show the last Item No. in the company and then will change to ‘The Boring Company’ to show its last Item No.


As you can see in this video, the last Item in Cronus is ‘SP-SCM3003’ and the last item in The Boring Company is ‘ZZZ001’:


Limitations

Now, be aware of validations and triggers. You shouldn´t modify with validations using ChangeCompany. As mentioned before, the scope of the triggers will not apply to the changed company. They will trigger on the company we are already in.

To illustrate it, imagine that we execute this piece of code. It will go to the first customer of ‘The Boring Company’ and modify the name of the first customer with validation.


The validation will execute the CreateCust() method on the trigger. Here is a simple code that will create customer 100.


However, customer ‘100’ will not be created in ‘The Boring Company’. It will be created for the company we are currently in. The scope of the trigger has not changed to the ‘Boring Company’.

For this, we can use the Task Scheduler. By placing the code in the ‘OnRun’ trigger of a codeunit and specifying the name of the company like the following.


Use Case Example


In this example, we will create a page to show approvals of a user from all companies. This way, users involved in approval workflows don´t have to change companies to check if there are pending approvals. They will see all of them in one place.

We will create a table called ‘Global Approval Entry’. The table will be a copy of the standard ‘Approval Entry’ but we will add a ‘Company’ field. Plus, will make it temporary.


Next, we will create a list page based on the new table, and on the ‘OnOpenPage’ trigger we will fill the table.


The way to fill the table is simple. We will loop through all companies and approval entries. Inserting in the table with ‘Transferfields’ and the company in that step of the loop.

We can filter the status of the approvals to show only pending or insert all of them. In this example we will only filter the ‘Approver ID’:


The result is a page where we can see all the approvals of the user and the company they come from. With all the details of the standard approval entry table.


As you can see, there are many ways to use this useful function. Make sure to add it to your coding backpack. You can read more about it in the Microsoft documentation below.

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-changecompany-method


That´s all. Hope you find it helpful.

How To Use ChangeCompany Function In Business Central

Post navigation


One thought on “How To Use ChangeCompany Function In Business Central

Leave a Reply

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