crossCompany in X++: The Query That Mixes Legal Entities
The statement went out with two customers named 1001. One was USMF. One was USSI. The clerk paid the USSI invoice against the USMF account because the grid had account numbers and no company column. The select that built the grid used crossCompany with no container. In a one-company dev box that query and a normal select return the same rows. In production the batch account can open twenty legal entities, and every one of them is in the result.
A select in X++ is scoped to the company the session is in. CustTable in USMF does not return USSI customers, because the kernel adds the current DataAreaId. The crossCompany keyword removes that filter. It does not remove security. The keyword returns data for all companies the user is authorized to read. A batch account that can open every legal entity will, and the report will look correct while it mixes them.
Put the company list in the statement
Pass a container. The query is then limited to that list, and still only to companies the user may read. The container can be a variable or an expression.
CustTable custTable;
container companies = ['USMF', 'USSI'];
while select crossCompany: companies custTable
order by custTable.DataAreaId, custTable.AccountNum
{
info(strFmt('%1 %2 %3',
custTable.DataAreaId,
custTable.AccountNum,
custTable.Name));
}
Anything you display or export from that loop has to carry DataAreaId. Account numbers are not globally unique. Item numbers are not globally unique. Vendor 1001 in USMF and vendor 1001 in USSI are different parties. A CSV with one column named Account is an incident waiting for a matching number.
An unconstrained while select crossCompany custTable is the version that shows up in incidents. The author tested it in a one-company dev box, where "all companies" and "this company" are the same set. Take the same class to a two-company UAT and the row count jumps. That jump is the bug.
The container should come from job parameters, not from curext(). Hard-coding ['USMF'] in a class that already runs inside USMF is how the next legal entity gets omitted on purpose, then someone removes the container "to make it work in DEMF" and you are back to unconstrained.
Reading across companies does not switch the session
crossCompany changes which rows a select can see. It does not change curext(), and it does not retarget number sequences, default dimensions, or inserts. Read a customer from USSI and then call SalesTable.insert() and the order is created in the company the session started in. The buffer's DataAreaId on the customer is USSI. The sales order's DataAreaId is whatever the session is. That mismatch is silent.
Writes that must land in another company go inside changeCompany, which switches the session for the block:
CustTable custTable;
SalesTable salesTable;
select crossCompany: ['USSI'] firstonly custTable
where custTable.AccountNum == '1001';
changeCompany('USSI')
{
salesTable.clear();
salesTable.initValue();
salesTable.CustAccount = custTable.AccountNum;
salesTable.initFromCustTable();
salesTable.insert();
// curext() is USSI here, including number sequences
}
Do not insert inside the crossCompany loop and assume the buffer company wins. It does not. Nesting changeCompany inside a crossCompany loop is legal and easy to get wrong: you switch, insert, and forget that the next select in the same loop is still the outer session's query, not a query in the switched company. Keep the read loop and the write loop as two phases if the code is doing both.
changeCompany is also the wrong tool for a report. Switching the session to print one row, then switching back, is slow and still easy to leave in the wrong company on an exception. Read with a container. Print DataAreaId. Leave curext() alone.
Query objects and OData are the same bug in other clothes
Query objects follow the same rule. allowCrossCompany(true) opens the query, and addCompanyRange puts the container back. A form or a report that sets the flag and never adds a range has the same bug as the unconstrained select.
Query q = new Query();
QueryBuildDataSource qbds = q.addDataSource(tableNum(CustTable));
q.allowCrossCompany(true);
q.addCompanyRange('USMF');
q.addCompanyRange('USSI');
QueryRun qr = new QueryRun(q);
while (qr.next())
{
CustTable custTable = qr.get(tableNum(CustTable));
// custTable.DataAreaId is required on anything you show
}
The same split exists on data entities. When PrimaryCompanyContext is set, X++ reads are filtered to the current company unless the query asks for cross-company data. OData reads are not filtered. The caller has to send dataAreaId itself. An integration that pages an entity with no company filter is a cross-company export whether or not anyone typed the keyword. That is also why a throttled OData client can look "random": it is walking every legal entity. The request limits in OData throttling assume you meant to read that much.
A filter of dataAreaId eq 'USMF' on the OData URL is the container. Without it, you get every company the service principal can open, paged as if it were one table. Matching on AccountNum across that stream will join the wrong legal entity the first time two companies share a number.
When you should not use crossCompany
Stay in the current company when the business process is already a company process: posting a journal, running a number sequence, printing a document that belongs to one legal entity. Cross-company is for the exception: a consolidation report, a global customer search the user asked for, a batch that is parameterized with an explicit list.
Do not use it to "see if the customer exists somewhere" and then update the first hit. The first hit is whichever DataAreaId sorts first, not the customer the user is looking at.
Do not grant the batch user every company so the unconstrained query "just works." The user on the batch job is the real scope. crossCompany cannot see a company that user cannot open, and it will see every company that user can. A service account granted System user in every legal entity is not a narrow integration account. Security roles still decide who may open a company at all; they are covered in roles, duties, and privileges. The keyword does not add a second check on top of that.
What to check before it ships
- Run the class in a two-company UAT with duplicate account numbers. If the output cannot tell them apart, it is not done.
- Confirm the company container is a parameter, logged at the start of the job.
- Confirm inserts sit inside
changeCompanyfor the company you mean to post to, and thatcurext()after the method returns to the caller is the company the caller expected. - For entities, confirm the OData caller sends
dataAreaId(or a legal-entity header your client already uses) and does not page the whole tenant. - For forms, confirm
allowCrossCompanyis paired withaddCompanyRange, and that the grid has a Company column.
The keyword is not a global search. It is a filter you took off. Put a smaller filter back, or the legal entities will mix in a way that only production has enough companies to show.
Keep reading
update_recordset: SQL Ran, and update() Never Did
update_recordset, insert_recordset, and delete_from send SQL now. They skip table methods, CoC, and events. Stamp the columns the method would have stamped, or stay on the row path.
FormDataSource.research: The Range You Added Never Ran
research reruns the live QueryRun. A range on query() waits for executeQuery. reread reloads only the current RecId, so an insert stays off the grid.
CacheLookup in X++: Found Serves the Stale Row Inside ttsbegin
Found serves the same CustTable inside ttsbegin. NotInTTS goes to SQL. EntireTable is for parameters. No unique index means the cache never hits.
Budget Control in Finance 10.0.49: Closed Periods, Reservations, and Broken Tracking
The budgeting features in Dynamics 365 Finance 10.0.49 — accounting-date advancement, reservation posting, encumbrance reporting, and a diagnostic for corrupt budget tracking.
Bank and Cash in Finance 10.0.49: Delayed Settlement, Match Preview, and Bridge Selection
What Dynamics 365 Finance 10.0.49 changes in payment journals and bank reconciliation, which pieces are still preview, and which bank behaviors you can no longer disable.
D365 F&O 10.0.49: What to Regression-Test Before the October Auto-Update
Finance and Supply Chain 10.0.49 (build 10.0.2790) is self-update in September 2026 and auto-update in October. Here is the feature-state triage that actually matters.
Newsletter
New posts, straight to your inbox
One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.
Comments
- No comments yet. Be the first.