Dynamics 365 Business Central: How to find field value by its text name (FieldName) – (Using codeunit 701 “Data Type Management”)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about how to find field value by its text name. For example, we have a field in Customer with the name “E-mail”, is there any way to find its value using only text name “E-mail”?

You can find all field info in the Field Virtual Table. The Field virtual table (ID 2000000041) contains information about fields in database tables.

You can force Business Central to run the table by adding the ?table=2000000041 parameter to the URL, such as in the following example: 
https://businesscentral.dynamics.com/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox01?table=2000000041

Note: The text name of the field is FieldName, not Field Caption.

Okay, this time we need to use codeunit 701 “Data Type Management”.

There is FindFieldByName function in it.

Let’s look at a simple example, using the text name ‘E-Mail’ to find its value.

Test Video:

Source Code:

pageextension 50111 ZYCustCardExt extends "Customer Card"
{
    trigger OnOpenPage()
    var
        DataTypeManagement: Codeunit "Data Type Management";
        RecRef: RecordRef;
        FldRef: FieldRef;
        Cust: Record Customer;
    begin
        if Cust.Get(Rec."No.") then begin
            if DataTypeManagement.GetRecordRef(Cust, RecRef) then
                if DataTypeManagement.FindFieldByName(RecRef, FldRef, 'E-Mail') then
                    Message(FldRef.Value);
        end;
    end;
}

We can create a more generic function for this. Give it a try!!!😁

Source Code:

pageextension 50111 ZYCustCardExt extends "Customer Card"
{
    trigger OnOpenPage()
    var
        Cust: Record Customer;
    begin
        if Cust.Get(Rec."No.") then begin
            Message(FindFieldValueByTextName(Cust, 'E-Mail'));
        end;
    end;

    local procedure FindFieldValueByTextName(RecVar: Variant; FieldName: Text[30]): Text
    var
        DataTypeManagement: Codeunit "Data Type Management";
        RecRef: RecordRef;
        FldRef: FieldRef;
    begin
        if DataTypeManagement.GetRecordRef(RecVar, RecRef) then
            if DataTypeManagement.FindFieldByName(RecRef, FldRef, FieldName) then
                exit(FldRef.Value);
    end;
}

Note: If FindFieldByName function returns false, for example, the text name does not exist, a runtime error occurs when using FieldRef.Value.

Microsoft.Dynamics.Nav.Runtime.NavFieldRef variable not initialized.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL