Temporary records are not returned
In a CodeUnit a function is provided which fills a temporary record and returns it. In the round up function one cannot iterate over this temporary record. One has access to exactly one record. I could not find any restriction in the documentation.
pageextension 50100 CustomerListExt extends "Customer List"
{
trigger OnOpenPage();
var
complexType: Codeunit ComplexType;
buffer1, buffer2 : Record Buffer;
msg: TextBuilder;
begin
buffer1 := complexType.GetBuffer();
buffer2 := complexType.GetBuffer(3);
Message('%1\%2\%3 > %4 [%5]', complexType.GetBuffer().Count(), buffer1.Count(), buffer1.I, buffer1.Amount, buffer1.Customer);
Message('%1\%2\%3 > %4 [%5]', complexType.GetBuffer(3).Count(), buffer2.Count(), buffer2.I, buffer2.Amount, buffer2.Customer);
msg.AppendLine('-Buffer 1-');
repeat
msg.AppendLine(StrSubstNo('%1 > %2 [%3]', buffer1.I, buffer1.Amount, buffer1.Customer));
until buffer1.Next() = 0;
msg.AppendLine('-Buffer 2-');
repeat
msg.AppendLine(StrSubstNo('%1 > %2 [%3]', buffer2.I, buffer2.Amount, buffer2.Customer));
until buffer2.Next() = 0;
Message(msg.ToText());
end;
}
table 50100 Buffer
{
TableType = Temporary;
fields
{
field(1; I; Integer) { }
field(2; Amount; Decimal) { }
field(3; Customer; code[20]) { }
}
}
codeunit 50100 ComplexType
{
procedure GetBuffer() buffer: Record Buffer temporary
var
customers: Record Customer;
i, total : Integer;
begin
total := 3;
i := 1;
repeat
if i = 1 then begin
customers.SetAutoCalcFields("Balance (LCY)");
customers.FindSet();
end else
customers.Next();
buffer.Init();
buffer.I := i;
buffer.Amount := customers."Balance (LCY)";
buffer.Customer := customers."No.";
buffer.Insert();
i += 1;
until i > total;
buffer.FindSet();
exit(buffer);
end;
procedure GetBuffer(total: Integer) buffer: Record Buffer;
var
customers: Record Customer;
i: Integer;
begin
i := 1;
repeat
if i = 1 then begin
customers.SetAutoCalcFields("Balance (LCY)");
customers.FindSet();
end else
customers.Next();
buffer.Init();
buffer.I := i;
buffer.Amount := customers."Balance (LCY)";
buffer.Customer := customers."No.";
buffer.Insert();
i += 1;
until i > total;
buffer.FindSet();
exit(buffer);
end;
}
Version: DE Business Central 18.3 (Plattform 18.0.27224.27469 + Anwendung 18.3.27240.27480) Version: AL Language Extension v7.3.486497
What if you drop the exit(buffer);? I think maybe it causes a copy of the record, as if passing by non-var, which would lose the inserted data/identity of the temp table and only keep the current field values.
There is no point doing exit(ResultVariable) in any case, since that is implied, but if my intuition on how it works is right, it might be actively harmful in cases like this.
If it weren't at the end of the function, change to exit; instead, which just says 'exit the function', rather than 'exit the function, assigning a copy of buffer to itself on the way out'.
@db-bc This might be more than intuition ;-) See https://www.waldo.be/2021/07/06/the-complexity-of-complex-return-types/, section "A named return variable, or not a named return variable, that’s the question!"
This variant was also tested, but without success. No temporary record is used in the waldo post.
Show the one you tested without the breaking exit(copy); then, so there's a useful code sample that doesn't have other problems.
Definition of the temporary table
table 50100 Buffer
{
TableType = Temporary;
fields
{
field(1; I; Integer) { }
field(2; Amount; Decimal) { }
field(3; Customer; code[20]) { }
}
keys
{
key(PK; I) { }
}
}
Usage
pageextension 50100 CustomerListExt extends "Customer List"
{
trigger OnOpenPage();
var
complexType: Codeunit ComplexType;
buffer: Record Buffer temporary;
msg: TextBuilder;
begin
buffer := complexType.GetBuffer(3);
msg.AppendLine(StrSubstNo('%1\%2\%3 > %4 [%5]', complexType.GetBuffer(3).Count(), buffer.Count(), buffer.I, buffer.Amount, buffer.Customer));
msg.AppendLine('-Buffer-');
repeat
msg.AppendLine(StrSubstNo('%1 > %2 [%3]', buffer.I, buffer.Amount, buffer.Customer));
until buffer.Next() = 0;
Message(msg.ToText());
end;
}
Experiment 1
codeunit 50100 ComplexType
{
procedure GetBuffer(total: Integer) buffer: Record Buffer temporary;
var
customers: Record Customer;
i: Integer;
begin
i := 1;
repeat
if i = 1 then begin
customers.SetAutoCalcFields("Balance (LCY)");
customers.FindSet();
end else
customers.Next();
buffer.Init();
buffer.I := i;
buffer.Amount := customers."Balance (LCY)";
buffer.Customer := customers."No.";
buffer.Insert();
i += 1;
until i > total;
buffer.Reset();
buffer.FindSet();
end;
}

Experiment 2
codeunit 50100 ComplexType
{
procedure GetBuffer(total: Integer): Record Buffer temporary;
var
customers: Record Customer;
buffer: Record Buffer temporary;
i: Integer;
begin
i := 1;
repeat
if i = 1 then begin
customers.SetAutoCalcFields("Balance (LCY)");
customers.FindSet();
end else
customers.Next();
buffer.Init();
buffer.I := i;
buffer.Amount := customers."Balance (LCY)";
buffer.Customer := customers."No.";
buffer.Insert();
i += 1;
until i > total;
buffer.Reset();
buffer.FindSet();
exit(buffer);
end;
}

@LutzRose Thank you for the well-described repro steps. We will investigate further.
complexType.GetBuffer(3).Count() returning 3 shows that the temporary record is returned just fine.
The problem is when you do buffer := complexType.GetBuffer(3), you are just assigning the single record from the result to buffer, but you are not copying the actual table with its dataset/records/filters/etc.
I wonder if you should instead be doing buffer.Copy(complexType.GetBuffer(3), true) where true is the ShareTable argument, which means
Specifies whether the method copies filters, views, automatically calculated FlowFields, marks, fields, and keys of the record or creates a reference to a temporary record. If FromRecord and Record are both temporary and ShareTable is true, then the COPY method causes Record to reference the same table as FromRecord. If ShareTable is true, then both Record and FromRecord must be temporary; otherwise an error will occur. The default value is false. If you specify false, only filters, marks, and keys are copied.
from https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-copy-method
Does that change anything?
I'd expect this also to affect non-temp tables, though. In which case, this should probably be documented as a gotcha in documentation for returning records - to only assign with := if you only care about the single record's values, otherwise to use Copy(src) if you care about filters, temp table references (in that case adding ShareTable := true), and anything else that would require var on an input argument.
Currently there is no workaround for this. Assigning a record returned from another record variable is the same as assigning one record variable to another record variable.
The workaround would be to use the Record.Copy (as mentioned), but the first parameter is declared as var and prevents the argument from being a record returned from a method. We are going to investigate if we can remove the var without breaking existing code. We don't have an ETA for this.
@esbenk Am I understanding correctly? The Copy method cannot be used for saving any records returned by a function (not just temporary ones)?
Yes, that is correct.
Hi @esbenk ,
Don't just look into the "copy"-function, but also take the "copyfilters" into consideration, if you don't mind.
Both functions take the record as input (hence the FromRecord):

The method doesn't change anything on the variable, so a var really isn't necessary, and in my limited knowledge can never break any code 🤔. Would be awesome to fix this, to enable these complex return types to behave as intended.
And while you're at it .. if we'd be able to do something like:
Foreach Rec in SalesHeader.SalesLines() do begin
Rec.DoSomething();
end;
that would be awesome as well 😉.
The method doesn't change anything on the variable, so a
varreally isn't necessary
It is if the method obeys the same rules that a user-defined procedure would, i.e. that without var it receives a copy of the variable, which in the case of a record drops all the filters/views/ShareTable/etc. - and means the end result is only a copy of the values of fields.
Of course, it's probably not the case that a platform function must follow the same logic, but maybe without other changes it currently would.
The lack of support for returning 'full' tables with all their state, and instead only single records, should IMHO be documented clearly and with clangorous warnings - as otherwise it's going to lead to broken code, as people's intuition here is fairly likely to be wrong or at least absent-minded.
@waldo1001:
And while you're at it .. if we'd be able to do something like:
Foreach Rec in SalesHeader.SalesLines() do begin Rec.DoSomething(); end;that would be awesome as well 😉.
Nice idea, but how would it work? Is it equivalent to writing this, I take it?, i.e. does a FindSet() for us, and does nothing if that was empty. I have often missed not having something like while Rec.Read() do instead of having to do 'if not empty then repeat until'.
Rec.Copy(SalesHeader.SalesLines());
if Rec.FindSet() then
repeat
Rec.DoSomething()
until Rec.Next() = 0
Hello everyone, thank you for being active in the discussion, this drives the language forward. We are changing the signature of Record.Copy and it will be able to receive RValues as arguments. The change will be first available in 18.4.
This will unblock the scnario:
procedure SomeProcedure()
var
ComplexType: Codeunit ComplexType;
buffer: Record Buffer temporary;
begin
buffer.Copy(complexType.GetBuffer(3),true);
end;
Sounds great, thanks to all for that!
P.S. As a big fan of C++, I like to see the term rvalue ;-)
Thanks for this! Looking forward to 18.4 in a few days to test this ;-)