microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
718 stars 242 forks source link

No way to differentiate between RecordRef.Copy when using Variant #7732

Open navdotnetreqs opened 2 months ago

navdotnetreqs commented 2 months ago

When using a variant for generic code, there doesn't seem to be a way to differentiate between overloaded RecordRef.Copy()-function calls.

If the variant was a recordref, I could do

sourcerecref:=variant;
recordref.copy(sourcerecref);

But in this case the variant is a Record, which is precisely why I need recref.copy.

"The call is ambiguous between the method 'Copy(var Table, [Boolean])' defined in Class 'RecordRef' by the extension '' and the method 'Copy(RecordRef, [Boolean])' defined in Class 'RecordRef' by the extension ''.AL[AL0196]"

A type "Table" does not exist and we cannot do a cast, so this can probably not be done unless the Copy-function is split into two..?

BazookaMusic commented 2 months ago

This probably requires a new overload for the copy function to handle the record. There's no generic record type to cast to

ernestasjuska commented 3 weeks ago

Hi, I think what you are looking for is this:

var
     MyTable: Record "My Table";
     MyTableRecordRef: RecordRef;

MyTableRecordRef.GetTable(MyTable);

GetTable is equivalent to Copy.

This works with both normal records and temporary records (table gets shared). Expand to see the code. ```al table 50100 "My Table" { fields { field(1; K; Integer) { } } } report 50100 "Test My Table" { UsageCategory = Tasks; ApplicationArea = All; ProcessingOnly = true; trigger OnPreReport() var MyTable: Record "My Table"; TempMyTable: Record "My Table" temporary; begin GlobalLanguage(1033); MyTable.Reset(); MyTable.DeleteAll(true); MyTable.Init(); MyTable.K := 1; MyTable.Insert(true); MyTable.Init(); MyTable.K := 2; MyTable.Insert(true); MyTable.Mark(true); MyTable.Init(); MyTable.K := 3; MyTable.Insert(true); MyTable.Mark(true); MyTable.SetFilter(K, '>%1', 0); MyTable.MarkedOnly(true); TempMyTable.Init(); TempMyTable.K := -1; TempMyTable.Insert(true); TempMyTable.Init(); TempMyTable.K := -2; TempMyTable.Insert(true); TempMyTable.Mark(true); TempMyTable.Init(); TempMyTable.K := -3; TempMyTable.Insert(true); TempMyTable.Mark(true); TempMyTable.Init(); TempMyTable.K := -4; TempMyTable.Insert(true); TempMyTable.Mark(true); TempMyTable.SetFilter(K, '<%1', 0); TempMyTable.MarkedOnly(true); Message( 'Normal: %1\\Temporary: %2', GetRecordInfo(MyTable), GetRecordInfo(TempMyTable)); end; local procedure GetRecordInfo( RecordVariant: Variant): Text var _RecordRef: RecordRef; begin _RecordRef.GetTable(RecordVariant); exit(StrSubstNo( 'IsTemporary: %1; Count: %2; Filters: %3', _RecordRef.IsTemporary, _RecordRef.Count, _RecordRef.GetFilters())); end; } ``` Output: ``` Normal: IsTemporary: No; Count: 2; Filters: Marked: Yes, K: >0 Temporary: IsTemporary: Yes; Count: 3; Filters: Marked: Yes, K: <0 ```