I'm working on optimizing some AL code in Dynamics Business Central, and I've noticed that there's a lot of redundant code when searching for records based on specific fields like name or age. I'd like to refactor this into a more generic function that can handle searching any table for any field.
procedure findEmployeeNo(Name:Text[20]; Age:Integer): code[20]
begin
// search by Name
Employee.Reset();
Employee.ChangeCompany(company.Name);
Employee.Setrange("Name", Name);
If Employee.FindFirst() then begin
exit(Employee."No.")
end;
// search by Age
Employee.Reset();
Employee.Setrange("Age", Age);
If Employee.FindFirst() then begin
exit(Employee."No.")
end;
exit('');
end;
procedure findEmployeeNo(Name:Text[20]; Age:Integer): code[20]
var Employee: record Employee;
begin
Employee := findFieldData('Employee', 'Name', Name)
If Employee then begin
exit(Employee."No.")
end;
Employee := findFieldData('Employee', 'Age', Age)
If Employee then begin
exit(Employee."No.")
end;
exit('');
end;
You can do it using
RecRef/FieldRefdata types and record variable ofFieldtable.But why? This will not give you any performance (probably the opposite), it will make code less readable and less searchable using tools like Statical Prism.
To much cons and not a single pro for this approach. Not worth it imho.