问题描述
枚举是/将成为Business Central 365的选件替代品。最近,我有一个机会可以使用它,并且可以弄湿我的脚。似乎经常发生这种情况,您所需的功能中约有80%随时可用,而其余的20%则需要的工作量更多。
对于枚举,您将获得名称的文本列表和关联的序数值的整数列表,但不会获得标题列表。在下面的部分示例Enum FourStates中,Default,OH和TX是名称,0、1和2是ordinals和空格,Ohio和Texas是标题。请注意,序数是定义的数值,而不是索引。在下面的示例中,完全有效的序数可以是1、5和7。
value(0; Default) { Caption = ' '; }
value(1; OH) { Caption = 'Ohio'; }
value(2; TX) { Caption = 'Texas'; }
如果将“表”或“页面”字段定义为枚举,则标题将显示在下拉列表中。要获取标题,可以使用Format(EnumType :: Name),但我们需要迭代给定枚举的所有标题。
解决方法
在浏览了一些博客和文档之后,下面是我发现的内容的摘要。
-
首先,存在一个主要限制,因为字幕只能在Enum类型的上下文中处理,并且至少到目前为止,Business Central 365 SaaS解决方案不支持通用Enum(例如,类似EnumRef RecordRef和FieldRef)。这意味着必须在逐个枚举的基础上创建字幕的文本列表。
-
但是,一旦创建了标题列表,然后使用[文本]的名称,顺序和标题列表的组合,您就可以编写适用于任何枚举的通用代码。
-
一个难题,当您在VS Code中使用TEnum代码片段时,它会将第一个元素的默认值设置为0。我们已经学习了将Default设置为上面的部分代码,或者将其更改为1。这样做的原因是因为如果有可能每个人都可以通过StrMenu命令使用一个枚举,则StrMenu默认为0作为Cancel返回值。在这种情况下,您永远不能选择0序数,因为ti将被视为已取消。
-
要创建在代码中使用的Enum的实例,请创建一个类似于MyChoices的Var变量:Enum“ Defined Choices”;。如果您还定义了MyChoice:Enum:“ Defined Choices”,则可以将它们与类似MyChoice = MyChoices :: FirstChoice then等的内容进行比较。
-
下面是一些具有一些不同Enum样式的示例代码,以及允许您为字幕创建[文本]列表的方法。同样,必须为每个特定的Enum进行编码。 一种。在VS Code中,使用AL Go创建一个新的HelloWorld应用 b。更改app.json文件的名称和发布者,我将其命名为EnumHelper 注意:我们总是定义一个发布者,因为您不能在SaaS中筛选默认发布者 C。用下面的代码替换HelloWorld.al文件中的所有代码 注意:为简化起见,下面的所有内容都在同一文件中 d。该代码是“会计科目表”页面的PageExtension,并在OnOpenPage触发器上运行 这样一来,无需大量设置代码即可轻松调用该代码。
-
这是允许您创建字幕列表的代码。变量myOrdinal是一个整数,而Flagged是一个已定义的Enum。请注意Enum :: EnumName,类似于Page :: PageName或Database :: TableName。
foreach myOrdinal in Flagged.Ordinals do begin // Enum definition,NOT an enum instance. captions.Add(Format(Enum::Flagged.FromInteger(myOrdinal))); end;
-
所有代码(抱歉,格式不正确)
枚举50200 FourStates { 可扩展=真;
value(0; Default) { Caption = ' '; }
value(1; OH) { Caption = 'Ohio'; }
value(2; TX) { Caption = 'Texas'; }
value(3; NC) { Caption = 'North Carolina'; }
value(4; IA) { Caption = 'Iowa'; }
value(5; MO) { Caption = 'Missouri'; }
}
枚举50201已标记 { 可扩展=真;
value(0; Default) { Caption = ' '; }
value(1; Bold) { Caption = 'Bold'; }
value(2; ITalic) { Caption = 'Italid '; }
value(4; Underline) { Caption = 'Underline'; }
value(8; BoldItalic) { Caption = 'Bold & Italic'; }
value(16; BoldUnderline) { Caption = 'Bold & Underline '; }
value(32; ItalicUnderline) { Caption = 'Italic & Underline'; }
value(64; All3Options) { Caption = 'All 3 Options'; }
}
枚举50202随机 { 可扩展=真;
value(0; Default) { Caption = ' '; }
value(7; Good) { Caption = 'The Good'; }
value(5; Bad) { Caption = 'The Bad'; }
value(11; Ugly) { Caption = 'The Ugly'; }
}
枚举50203 ProcessFlowOptions { 可扩展=真;
value(0; Default) { Caption = ' '; }
value(1; Flagged) { Caption = 'Flagged'; }
value(2; Randomized) { Caption = 'Randomized'; }
value(4; FourStates) { Caption = 'FourStates'; }
}
pageextension 50200“帐户图表EH”扩展了“帐户图表” { 变种 //枚举实例变量。 myFlagged:枚举已标记; myFourStates:枚举FourStates; myRandomized:随机枚举;
trigger OnOpenPage();
begin
case UserID.ToLower() of
'larry':
Message('Hello Larry,this is an extension for October testing.');
'vicki':
Message('Good morning Vicki,this is an extension for October testing.');
else
if Confirm('Hello %1 from EnumHelper.\\Click Yes to process or no to cancel.',true,UserID) then begin
ProcessEnumerations();
end;
end;
end;
local procedure ProcessEnumerations()
var
allLines: TextBuilder;
randomCaptions: List of [Text];
flaggedCaptions: List of [Text];
fourStatesCaptions: List of [Text];
begin
GetEnumCaptions(randomCaptions,flaggedCaptions,fourStatesCaptions);
IterateEnumNamesOrdinalsAndCaptions(allLines,randomCaptions,fourStatesCaptions);
Message(allLines.ToText());
end;
local procedure GetEnumCaptions(randomCaptions: List of [Text]; flaggedCaptions: List of [Text]; fourStatesCaptions: List of [Text])
begin
GetCaptions(randomCaptions,ProcessFlowOptions::Randomized);
GetCaptions(flaggedCaptions,ProcessFlowOptions::Flagged);
GetCaptions(fourStatesCaptions,ProcessFlowOptions::FourStates);
end;
local procedure IterateEnumNamesOrdinalsAndCaptions(allLines: TextBuilder; randomCaptions: List of [Text]; flaggedCaptions: List of [Text]; fourStatesCaptions: List of [Text])
begin
IterateEnumNamesOrdinalsAndCaptions('Flagged Enum',allLines,myFlagged.Names,myFlagged.Ordinals,flaggedCaptions);
IterateEnumNamesOrdinalsAndCaptions('Randomized Enum',myRandomized.Names,myRandomized.Ordinals,randomCaptions);
IterateEnumNamesOrdinalsAndCaptions('FourStates Enum',myFourStates.Names,myFourStates.Ordinals,fourStatesCaptions);
end;
local procedure IterateEnumNamesOrdinalsAndCaptions(title: Text; allLines: TextBuilder; enumNames: List of [Text]; enumOrdinals: List of [Integer]; enumCaptions: List of [Text])
var
i: Integer;
enumLine: TextBuilder;
enumLines: TextBuilder;
begin
allLines.AppendLine(title);
allLines.appendLine();
for i := 1 to enumNames.Count do begin
Clear(enumLine);
enumLine.AppendLine('EnumName: ''' + enumNames.Get(i) + ''',');
enumLine.AppendLine('EnumOrdinal: ' + Format(enumOrdinals.Get(i)) + ',');
enumLine.AppendLine('EnumCaption: ''' + enumCaptions.Get(i) + '''.');
//enumLine.AppendLine('EnumName: ''' + enumNames.Get(i) + ''',EnumOrdinal: ' + ordinal + ',EnumCaption: ''' + enumCaptions.Get(i) + '''');
enumLines.AppendLine(enumLine.ToText());
end;
allLines.AppendLine(enumLines.ToText());
end;
local procedure GetCaptions(captions: List of [Text]; processFlowOption: Enum ProcessFlowOptions)
var
myOrdinal: Integer;
myProcessFlowOptions: Enum ProcessFlowOptions;
begin
// Load captions by iterating specific Enums.
case processFlowOption of
myProcessFlowOptions::Flagged:
begin
foreach myOrdinal in Flagged.Ordinals do begin
// Enum definition,NOT an enum instance.
captions.Add(Format(Enum::Flagged.FromInteger(myOrdinal)));
end;
end;
myProcessFlowOptions::Randomized:
begin
foreach myOrdinal in Randomized.Ordinals do begin
// Enum definition,NOT an enum instance.
captions.Add(Format(Enum::Randomized.FromInteger(myOrdinal)));
end;
end;
myProcessFlowOptions::FourStates:
begin
foreach myOrdinal in FourStates.Ordinals do begin
// Enum definition,NOT an enum instance.
captions.Add(Format(Enum::FourStates.FromInteger(myOrdinal)));
end;
end;
end;
end;
}
享受
,我为BC14实施了更好的解决方法。这也应该适用于较新的版本,但是我仅在BC14上进行了测试。
var
RecRef: RecordRef;
FRef: FieldRef;
OptionNames: List of [Text];
OptionCaptions: List of [Text];
i: Integer;
RecRef.GetTable(Rec); // Some record
FRef := RecRef.Field(20); // Some field of type Enum
OptionNames := FRef.OptionMembers().Split(',');
OptionCaptions := FRef.OptionCaption().Split(',');
for i := 1 to OptionNames.Count() do begin
Evaluate(FRef,OptionNames.Get(i));
Message('Ordinal = %1\Name = %2\Caption = %3',format(FRef,9),OptionNames.Get(i),1)); // or OptionCaptions.Get(i)
end;