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

Notifications

Announcements

No record found.

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 111
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
    95,729 Super User 2025 Season 2 on at
    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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,362

#2
YUN ZHU Profile Picture

YUN ZHU 867 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans