KEYINDEX Function (RecordRef)

Gets the KeyRef of the key that has the index specified in the table that is currently selected. The key can be composed of fields of any supported data type. Data types that are not supported include BLOBs, FlowFilters, variables, and functions. If the sorting key is set to a field that is not part of a key, then the KEYINDEX is -1.


Key := RecordRef.KEYINDEX(Index)  


Type: RecordRef

The RecordRef that identifies the record and the table in which you are interested.

Type: Integer

The number of the index in which you are interested.

Property Value/Return Value

Type: KeyRef

The KeyRef of the field that has the specified index.


The first key in the index must have index 1, the second index 2, and so on. The last key must have index = KEYCOUNT. If the specified index is out of the range or if no table is selected, the function returns an error.


The following example opens table 18 (Customer) as a RecordRef variable that is named CustomerRecref. The loop starts from 1 and loops through the key indexes that are in the table. CustomerRecref.KEYCOUNT returns the maximum number of keys that are defined in the table. The loop continues until the last key is reached. For each index that is specified, the KEYINDEX function retrieves the KeyRef for the specified index. The key index and the KeyRef for the specified indexes are displayed in a message box. This example requires that you create the following variables and text constant in the C/AL Globals window.

Variable name DataType
CustomerRecref RecordRef
varKeyRef KeyRef
i Integer
Text constant name DataType ENU value
Text000 Text KeyIndex: %1 KeyRef: %2

  FOR i := 1 TO CustomerRecref.KEYCOUNT DO BEGIN  
    varKeyRef := CustomerRecref.KEYINDEX(i);  
    MESSAGE(Text000, i, varKeyRef);  

See Also

RecordRef Data Type