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...
Suggested answer

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

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

```
  • Suggested answer
    YUN ZHU Profile Picture
    81,794 Super User 2025 Season 1 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,017 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans