web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

searching any table for any field in procedure Dynamics Business Central AL

(0) ShareShare
ReportReport
Posted on by 59
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;

```
Refactoring AL Code to:
```
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;

```
I have the same question (0)
  • Verified answer
    YUN ZHU Profile Picture
    93,222 Super User 2025 Season 2 on at
    searching any table for any field in procedure Dynamics Business Central AL
    Hi, sorry, what is your problem?
    If you want to modify the standard code, this is not possible on SaaS.
    If you add new methods, you can use them in your custom functions. This is no problem.
    If you want to write more generic function, it is recommended to use RecordRef Data Type and FieldRef Data Type.
     
    Hope this helps.
    Thanks.
    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 2,828

#2
Sumit Singh Profile Picture

Sumit Singh 2,617

#3
YUN ZHU Profile Picture

YUN ZHU 1,861 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans