Tech Tip: Check if the Record is Locked
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: April 13, 2018
It is important when programatically modifying (updating or deleting) a record to ensure that the record is not locked, otherwise the changes being made will not be reflected in the database! A locked record modified will not have the modified values change, and a locked record deleted will remain in the database.
To check if a record is locked, the 4D command Locked can be used simply by passing in the table in question, where the method checks if the current record in the selection is locked and returns a boolean value True if the the record is locked or False if the record is not locked.
To check if the current record for a table is locked, simply run:
Locked([Table_Name]) |
So if, for example, deleting the first 10 records of a table is required programatically then a method doing that could look like this, creating an array of records locked during the deletion which can then be checked at a later time and removed as needed.
ARRAY LONGINT($records_not_deleted;0) QUERY([Table_1];[Table_1]ID<10) For ($i;1;Records in selection([Table_1])) If (Not(Locked([Table_1]))) DELETE RECORD([Table_1]) Else // Record is locked! APPEND TO ARRAY($records_not_deleted;[Table_1]ID) End if NEXT RECORD([Table_1]) End for // Handle the locked record(s) from $records_not_deleted array |
If a record is locked, handling the locked record may require more information about which process has locked the record. If more information about the locked record is required, the 4D command Get locked records info will return an object for all locked records in the table, providing useful information such as the process and username which have the record(s) locked.