# KeyRef.FieldCount() Method
> Version: _Available or changed with runtime version 1.0._
Gets the number of fields that have been defined in a key. Returns an error if no key is selected.
## Syntax
```al
No :=   KeyRef.FieldCount()
```
> NOTE
> This method can be invoked using property access syntax.
## Parameters
KeyRef
Type: keyref-data-type
An instance of the keyref-data-type data type.
## Return Value
No
Type: ../integer/integer-data-type
The number of fields that have been defined in the key.
## Example
The following example retrieves the number of fields that are defined in a key in record. The table with ID 18 \(the Customer table\) is open with a reference to table 18. The KeyIndex Method \(RecordRef\)<../library> method retrieves the second key in the record and store the KeyRef in the varKeyRef variable. The FieldCount Method \(KeyREF\)<../library> is then used to return the number of fields defined in the key and displayed in a message box.
```al
var
    RecRef: RecordRef;
    varKeyRef: KeyRef;
    VarCount: Integer;
begin
    RecRef.Open(18);
    varKeyRef := RecRef.KeyIndex(2);
    VarCount := varKeyRef.FieldCount;
    Message('The number of fields defined in the key is: %1', VarCount);
end;
```
## Related information
keyref-data-type
Get Started with AL<../../devenv-get-started>
Developing Extensions<../../devenv-dev-overview>

Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/keyref/keyref-fieldcount-method, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact. https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/keyref/keyref-fieldcount-method/
