# KeyRef.FieldIndex(Integer) Method
> Version: _Available or changed with runtime version 1.0._
Gets the FieldRef of the field that has this index in the key referred to by the KeyRef variable. Returns an error if no key is selected.
## Syntax
```al
Field :=   KeyRef.FieldIndex(Index: Integer)
```
## Parameters
KeyRef
Type: keyref-data-type
An instance of the keyref-data-type data type.
Index
Type: ../integer/integer-data-type
The input index.
## Return Value
Field
Type: ../fieldref/fieldref-data-type
The FieldRef that refers to the field with this index in the key.
## Remarks
The first field in the index must have index 1, the second index 2, and so on. The last field must have index = FieldCount. If the index is out of the range supplied or if no table is selected, the method returns an error.
## Example
The following example displays the caption of a field in a record. The table with ID 18 \(the Customer table\) is open with a reference. The Keyref for the record is retrieved by using the KeyIndex Method \(RecordRef\)<../library>. The method retrieves the second key in the record and stores the value in the varKeyRef variable. The varKeyRef variable is then used to return the FieldRef. The varFieldRef variable is used to display the caption of the field.
```al
var
    RecRef: RecordRef;
    varKeyRef: KeyRef;
    varFieldRef: FieldRef;
begin
    RecRef.Open(18);
    varKeyRef := RecRef.KeyIndex(2);
    varFieldRef := varKeyRef.FieldIndex(1);
    Message('The Field name is: %1' ,varFieldRef.Caption);
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-fieldindex-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-fieldindex-method/
