Why does my X++ TempDB temp table buffer lose values after returning from a method when I iterate using the buffer?
I’m seeing unexpected blank/missing values when I populate a TempDB-backed temporary table buffer in one X++ method, pass/return that buffer (or the temp table buffer) to the caller, and then iterate over it.
Inside the method, I use a temp table buffer as an iterator (e.g., filling records, then looping over it). After the method returns and the caller continues, some fields are no longer what I set—they appear to be overwritten or cleared.
Here’s a simplified pattern of what I’m doing:
public void buildTemp()
{
MyTempTable tmp;
MyTempTable tmpIter;
// Populate TempDB temp table
tmp.clear();
tmp.Field1 = 'A';
tmp.insert();
tmp.Field1 = 'B';
tmp.insert();
// Iterate using a buffer
while select tmpIter
{
tmpIter.Field2 = tmpIter.Field1;
tmpIter.update();
}
// tmp is exposed to caller in some way (e.g., returned/used after this method)
}I expected the caller to still see the populated/updated TempDB records, but after leaving the method the caller sees missing/blank values.
What’s the correct X++ approach to reuse/iterate TempDB temp table buffers across method boundaries so the caller can reliably read the populated values?
1 answer
Write an answerWhy values “disappear” after returning from a method#
For TempDB temp tables, the data lives in the session-scoped TempDB and the buffer you hold in X++ is just a client-side representation of “the current cursor/record”.
Two common things cause the symptom you describe:
- You’re iterating with the same temp table buffer / cursor lifetime assumptions are wrong (especially if you modify records while iterating).
- In X++,
while select tmpIter from ...drives a query cursor. If you thenupdate()the same table/record in the loop, the buffer’s identity/cached fields can easily lead to confusing results if the caller is reading a different buffer instance or a stale cached record.
- In X++,
- You don’t re-read/refresh on the caller side after the method finishes.
- TempDB is not “magically synchronized” to every buffer instance you created before the update. If the caller still has a buffer that was positioned before the update (or it’s using another buffer variable that wasn’t reselected), fields can appear blank/old.
Key point: In D365FO, a temp table buffer is not a pass-by-value dataset. If you want the caller to reliably read updated TempDB rows, the caller must select/requery (or refresh) after the method that modifies the temp table.
Correct approach#
Option A (recommended): Pass the temp table buffer by reference and re-select/refresh in the caller#
Use a var parameter so both caller and callee operate on the same temp table instance, and then ensure the caller iterates with its own select (or refreshes its cursor).
public void buildTemp(MyTempTable _tmp)
{
_tmp.clear();
_tmp.Field1 = 'A';
_tmp.insert();
_tmp.Field1 = 'B';
_tmp.insert();
// Use a separate iterator buffer to avoid cursor/update confusion
MyTempTable tmpIter;
while select tmpIter
where tmpIter.Field1 != ''
{
tmpIter.Field2 = tmpIter.Field1;
tmpIter.update();
}
}Caller:
MyTempTable tmp;
buildTemp(tmp); // modifies TempDB rows
// Re-select AFTER the method so you’re not looking at a stale buffer cursor
MyTempTable tmpRead;
while select tmpRead
{
info(strFmt('%1 %2', tmpRead.Field1, tmpRead.Field2));
}This works because:
- The callee updates TempDB rows.
- The caller iterates using a fresh select, so it sees the updated values.
Option B: If the caller already has a positioned buffer, call refresh() before reading fields#
If you must keep using the same buffer variable instance, you generally need to refresh() it (and only after you know which record it should represent).
// In caller, after buildTemp()
myBuffer.refresh();
// now read fieldsHowever, for most temp-table consumption patterns, Option A (re-select/iterate in caller) is safer and clearer.
Important guidance about iterating + updating#
When populating and then updating a TempDB temp table:
- Prefer separate buffers:
- one buffer for inserting/initial creation
- a different buffer for
while select ... update()
- Avoid using the same buffer variable as both the driving cursor and the buffer you insert/update in a way that changes what the cursor is reading.
Your sample already uses tmpIter for the update loop—that part is fine. The missing piece is usually what the caller is reading after the method returns (stale buffer vs re-select).
Edge cases to watch#
- Batch vs interactive: TempDB temp tables still work, but don’t assume you can pass buffers across execution contexts (e.g., different tasks/batches). Your scenario sounds interactive, but watch for this if it’s triggered from batch.
- Security / cross-company: TempDB rows aren’t “company-isolated” automatically the way normal data is; if your logic changes
crossCompany/data areas, your select ranges might differ. - Large datasets: Updating row-by-row with
while select ... update()is slower than set-based operations. If this logic runs on many rows, consider a set-based update pattern, but don’t do it until correctness is nailed down.
Bottom line#
Don’t try to “hand over a buffer” and assume the caller will automatically see updated fields. For TempDB temp tables:
- Update rows in TempDB in the method.
- In the caller, iterate using a new
while select(preferred) or callrefresh()on the specific buffer/record you intend to read.
That is the reliable X++ approach across method boundaries.
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer