Hi, Readers.
Yesterday I saw an interesting question on the D365 forum, how to use field name to find field id. More details: finding the id of a field using the field name only by using the recref and fieldref
So, in this post, I would like to talk about how to get the id, type, length and value of a field by its text name (field name).
As you might know, we can use RecordRef Data Type and FieldRef Data Type to simply get Field Name or Field Caption from Field Id.
For example: FieldRef.Name() or FieldRef.Caption()
But what about the opposite? The first thing that comes to mind is to use the table 2000000041 “Field”, because it records all the table and field information. More details: How to export all fields in all tables in Dynamics 365 Business Central
For example, E-Mail (102, Text[80])
Of course, using this way can also get the length, type and other information of the field, but it cannot get the value.
In fact, there is another convenient way, using codeunit 701 “Data Type Management”.
There is FindFieldByName method in it.
Let me do a simple example.
Very simple, and these data are stored in RecordRef and FieldRef, you can do further processing. Give it a try!!!😁
Source code:
pageextension 50112 ZYCustCardExt extends "Customer Card"
{
trigger OnOpenPage()
var
DataTypeManagement: Codeunit "Data Type Management";
RecRef: RecordRef;
FldRef: FieldRef;
Cust: Record Customer;
Msg: Label 'Field Name is ''%1''\Field Id is ''%2''\Field Type is ''%3''\Field Length is ''%4''';
begin
if Cust.Get(Rec."No.") then begin
if DataTypeManagement.GetRecordRef(Cust, RecRef) then
if DataTypeManagement.FindFieldByName(RecRef, FldRef, 'E-Mail') then begin
Message(Msg, FldRef.Name, FldRef.Number, FldRef.Type, FldRef.Length);
end;
end;
end;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント