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;
```