✨ This blog originally appeared here: View original blog
Modular Data Access Across Companies Using ChangeCompany for Real-Time Reads
I was thinking about what might truly add value for the next volume of #BCSaturdayCodeHacks, and it brought me back to an earlier client engagement. The setup was a multi-entity—a well-known food chain operating through a parent company with several subsidiaries. One requirement stuck with me: the ability to sync a G/L Account (Chart of Accounts) from the parent into each sub-company with just a button click.
It wasn’t about bulk imports or table hacks—it needed to be modular, traceable, and fast. That’s when I turned to something I’ll be showcasing today: ChangeCompany, a native AL method that unlocked clean, cross-company data access in Business Central.
Overview
In AL development, one overlooked superpower is the ability to pull data across companies—without breaking architecture. The ChangeCompany method allows developers to dynamically read (or write, with nuance) records from another company within the same database. This article is your definitive guide to using ChangeCompany in clean, modular, and scalable ways.
What Is ChangeCompany?
Record.ChangeCompany(CompanyName: String) lets you temporarily switch the context of a record to another company.
Codeunit: JB ChangeCompany Mgt. – The Sync Logic
procedure SyncGLAccountToSubs(var SourceGL: Record "G/L Account")
var
Company: Record Company;
DestinationGL: Record "G/L Account";
GLAccountSyncToDestCompanyMsg: Label 'G/L Account synced to %1';
DestinationCompany: Text[30];
begin
Clear(DestinationCompany);
Company.SetFilter(Name, '<>%1', CompanyName);
if Page.RunModal(PAGE::Companies, Company) = Action::LookupOK then begin
DestinationCompany := Company.Name;
if Company.ChangeCompany(DestinationCompany) then
DestinationGL.ChangeCompany(DestinationCompany);
if not DestinationGL.Get(SourceGL."No.") then begin
DestinationGL.Init();
DestinationGL.TransferFields(SourceGL);
DestinationGL.Insert();
Message(GLAccountSyncToDestCompanyMsg, DestinationCompany);
end;
end;
end;
Highlights:
- Filters out the current company
- Prompts user with modal selection
- Switches context using ChangeCompany
- Copies and syncs record only if missing
Page Extension: Add the Copy Action to the G/L Account Card
pageextension 50101 "JB G/L Account Card" extends "G/L Account Card"
{
actions
{
addafter("Apply Template")
{
action("JB Copy G/L Account")
{
Caption = 'Copy G/L Account to Selected Company';
Image = CopyCostBudget;
Promoted = true;
PromotedCategory = Process;
trigger OnAction()
var
ChangeCompanyMgt: Codeunit "JB ChangeCompany Mgt.";
begin
ChangeCompanyMgt.SyncGLAccountToSubs(Rec);
end;
}
}
}
}
📌 This action adds a promoted button to the G/L Account Card, allowing users to trigger cross-company sync instantly.
Use Cases
Best Practices
- Read-Only Precision: Prefer reads over writes; writes require caution and proper context resets.
- Avoid Nesting: Nested ChangeCompany calls may lead to confusion or unexpected behavior.
- Filter First: Always apply filters before calling ChangeCompany()—this minimizes overhead.
- Wrap With Purpose: Create helper procedures to encapsulate logic and keep code DRY.
✨ Demo in Action:
🧠 Takeaways & Lessons Learned
- ChangeCompany enables clean, controlled access to other tenants—no direct table manipulation needed.
- Modal selection adds user accountability and reduces misrouting risks.
- TransferFields + Init offers a lightweight way to clone records while preserving data structure.
- Ideal for syncing master data like G/L Accounts, Dimensions, and Vendors.
💡 Gotchas to Watch For
- Transaction Confusion: Mixing ChangeCompany() with writes can complicate commits and rollbacks.
- Permissions: Ensure users have rights to access records in the target company.
- Structural Differences: Don’t assume identical data configurations across companies.
📌 Closing Thought
Whether you're syncing master data across legal entities, replicating templates, or building multi-company automation—ChangeCompany is a powerful ally for Business Central developers. This pattern scales cleanly without brittle endpoints or overengineered service layers.
Stay tuned as we continue modularizing AL patterns, echoing real-world consulting work, and delivering practical solutions for Business Central.
♻️ Repost to support the community and follow Jeffrey Bulanadi for clear, technical insights into Business Central and AL development.
🔗 Demo Repository: Explore my GitHub sample showcasing ChangeCompany in action: 👉 ChangeCompanyDemo on GitHub
Helpful Reference:
Record.ChangeCompany([Text]) Method - Business Central | Microsoft Learn
#BCSaturdayCodeHacks #Dynamics365 #MSDyn365 #MSDyn365BC #businesscentral #ALDevelopment #DevTips #ChangeCompany #BCSaturdayCodeHacks
Join the Conversation
Share your thoughts, ask questions, or discuss this article with the community. All comments are moderated to ensure quality discussions.
Loading comments...
No comments yet
Be the first to start the conversation!
0 Comments
Leave a Comment