Did you know that there is more than one way to get a single record in Microsoft Dynamics 365 BC?
1st Way to Get a Record
We all know to do a GET of one record based on the values of the primary key fields. The standard syntax is as follows.
[Ok :=] Record.Get(PK1[Value], PK2[Value],...).
An example would be:
SalesHeaderFind.FindFirst(); SalesHeaderGet.Get(SalesHeaderFind."Document Type", SalesHeaderFind."No.");
The most used one to get a record in Dynamics 365 BC, isn’t it?
What if I tell you that there is a way to make this code shorter?
2nd Way to Get a Record
SalesHeaderGet.Get(SalesHeaderFind.RecordId);
Will it work?
Yes! It will! This is because RecordID is already the primary key itself and not one of the fields that form it, as the method expects.
Also, be aware that in case the primary key field is of type RecordID, you cannot use GET. In this case, you will have to use the SetRange method.
3rd Way to Get a Record
And the last one, as of Business Central 2019 release wave 2, there is a new method, GetBySystemId. The syntax is as follows.
[RecordExists := ] Record.GetBySystemId(SystemId: Guid)
An example would be:
SalesHeaderGet.GetBySystemId(SalesHeaderFind.SystemId);
Every record in Dynamics 365 BC has a SystemId, which is stored in the SystemId field of the table. The SystemId cannot be changed.
Here is an example to try yourself:
procedure ThreeWaysToGetARecord() var SalesHeaderFind: Record "Sales Header"; SalesHeaderGet: Record "Sales Header"; begin SalesHeaderFind.FindFirst(); //GET by Specific Value SalesHeaderGet.Get(SalesHeaderFind."Document Type", SalesHeaderFind."No."); Message('GET by Specific Value ' + Format(SalesHeaderGet."No.")); //GET by RecordID SalesHeaderGet.Get(SalesHeaderFind.RecordId); Message('GET by RecordID ' + Format(SalesHeaderGet."No.")); //GET by SystemID SalesHeaderGet.GetBySystemId(SalesHeaderFind.SystemId); Message('GET by GetBySystemId ' + Format(SalesHeaderGet."No.")); end;