Skip to content

Instantly share code, notes, and snippets.

@martonsagi
Created October 24, 2019 11:55
Show Gist options
  • Save martonsagi/503d39e8f0d742fbf6865e96f63321a1 to your computer and use it in GitHub Desktop.
Save martonsagi/503d39e8f0d742fbf6865e96f63321a1 to your computer and use it in GitHub Desktop.
AL RecordHelper
var
RecHelper: Codeunit RecordHelper_DSK;
...
RecHelper.Open('Customer');
while RecHelper.Next() do begin
Message('%1', RecHelper.GetField('Balance').AsDecimal());
end;
RecHelper.Close();
...
codeunit xx RecordHelper_DSK
{
procedure Open(TableId: Integer)
begin
Open(TableId, false, CompanyName());
end;
procedure Open(Name: Text): Boolean
begin
exit(Open(Name, false, CompanyName()));
end;
procedure Open(Name: Text; Temp: Boolean; Company: Text): Boolean
var
AllObjs: Record AllObjWithCaption;
begin
AllObjs.SetRange("Object Type", AllObjs."Object Type"::Table);
AllObjs.SetRange("Object Name", Name);
if not AllObjs.FindFirst() then begin
AllObjs.SetRange("Object Name");
AllObjs.SetRange("Object Caption", Name);
end;
if not AllObjs.FindFirst() then
exit(false);
Open(AllObjs."Object ID", Temp, Company);
exit(true);
end;
procedure Open(TableId: Integer; Temp: Boolean; Company: Text)
begin
RecRef.Open(TableId, Temp, Company);
end;
procedure Close()
begin
RecRef.Close();
end;
procedure SetRecRef(Ref: Variant)
begin
Clear(RecRef);
if Ref.IsRecord() then
RecRef.GetTable(Ref)
else
RecRef := Ref;
end;
procedure GetRecRef(var Ref: Variant)
begin
if Ref.IsRecord() then
RecRef.SetTable(Ref)
else
Ref := RecRef;
end;
procedure GetField(Name: Text; var Result: Variant): Boolean
var
FRef: FieldRef;
begin
if not GetField(Name, FRef) then
exit(false);
If FRef.Class() = FieldClass::FlowField then
FRef.CalcField();
Result := FRef.Value();
end;
procedure GetField(Name: Text): JsonValue
var
FRef: FieldRef;
Result: JsonValue;
FVar: Variant;
begin
Clear(Result);
if not GetField(Name, FVar) then
exit(Result);
FVar := FRef.Value();
Result.ReadFrom(Format(FVar));
exit(Result);
end;
procedure GetField(Name: Text; var FRef: FieldRef): Boolean
var
i: Integer;
c: Integer;
begin
c := RecRef.FieldCount();
for i := 1 to c do
if RecRef.FieldExist(i) then begin
FRef := RecRef.FieldIndex(i);
if FRef.Name() = Name then
exit(true);
end;
exit(false);
end;
procedure SetField(Name: Text; Input: Variant): Boolean
var
FRef: FieldRef;
begin
if not GetField(Name, FRef) then
exit(false);
FRef.Value(Input);
end;
procedure Next(var Rec: Variant): Boolean
begin
SetRecRef(Rec);
if RecRef.Next() <> 0 then begin
GetRecRef(Rec);
exit(true);
end else
exit(false);
end;
procedure Next(): Boolean
begin
if RecRef.Next() <> 0 then
exit(true)
else
exit(false);
end;
var
RecRef: RecordRef;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment