{"title":"Record.SetCurrentKey(Any [, Any,...]) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/record/record-setcurrentkey-method/","by":"JOP-AL","license":"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/record/record-setcurrentkey-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.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setcurrentkey-method","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Selects a key for a table.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":1,"title":"Record.SetCurrentKey(Any [, Any,...]) Method","slug":"recordsetcurrentkeyany-any-method","at":0,"chars":2374},{"level":2,"title":"Syntax","slug":"syntax","at":134,"chars":84},{"level":2,"title":"Parameters","slug":"parameters","at":218,"chars":172},{"level":2,"title":"Return Value","slug":"return-value","at":390,"chars":238},{"level":2,"title":"Remarks","slug":"remarks","at":628,"chars":1612},{"level":2,"title":"Related information","slug":"related-information","at":2240,"chars":134}],"text":"# Record.SetCurrentKey(Any [, Any,...]) Method\n> Version: _Available or changed with runtime version 1.0._\nSelects a key for a table.\n## Syntax\n```al\n[Ok := ]  Record.SetCurrentKey(Field1: Any [, Field2: Any,...])\n```\n## Parameters\nRecord\nType: record-data-type\nAn instance of the record-data-type data type.\nField1\nType: ../any/any-data-type\n*[Optional] Field2*\nType: ../any/any-data-type\n## Return Value\n*[Optional] Ok*\nType: ../boolean/boolean-data-type\ntrue if the operation was successful; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.\n## Remarks\nYou can use SetCurrentKey to hint a sort order to the Business Central server. With the fields suggested in SetCurrentKey, the Business Central server then searches available key definitions and adds an ORDER BY clause with the fields from the key to the SQL statement issued to the database.\nA key matches the fields passed to SetCurrentKey if the key fields are either an exact match, or if the fields are a prefix of the key fields.\nIf no key is found that matches the fields given to SetCurrentKey, the sort order will still be applied but without a key, the query may be slower.\nWhen you use SetCurrentKey, the following rules apply:\n You can't use SetCurrentKey on:\n   FlowFilter fields\n   Nested FlowFields\n   FlowFields on ExternalSQL tables\n   Blob fields\n   FlowFields on virtual tables\n   FlowFields that query across application database / tenant database\n   FlowFields that query different table types\n  These fields aren't sortable. If any of these fields are used in the method, it returns false at runtime and an error occurs if not handled.\n Only active keys are considered.\n When the system searches for a key, the first occurrence of a key that starts with the specified fields is selected, which means:\n   Fields that are part of a IncludedFields definition aren't used when searching for a matching key.\n   If you specify only one field as a parameter when you call SetCurrentKey, the key that is selected might consist of more than one field.\n   If the field that you specify is the first component of several keys, the key that is selected might not be the key that you expect.\n## Related information\nrecord-data-type\nGet started with AL<../../devenv-get-started>\nDeveloping extensions<../../devenv-dev-overview>"}