Deletes a record in a table.
[Ok := ] RecordRef.Delete([RunTrigger: Boolean])
Specifies whether the code in the OnDelete trigger will be executed. If this parameter is true, the code will be executed. If this parameter is false, then the code in the OnDelete trigger is not executed. The default value is false. This parameter is optional.
true 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.
The current key and any filters on the record do not affect this operation. The record to delete is identified by the values in its primary key.
If an end-user modifies a record between the time that another end-user or another process reads the record and modifies it, then the second user must refresh the value of the record variable before editing the record. Otherwise, the end-user receives the following run-time error:
Another user has modified the record for this <Table Name> after you retrieved it from the database.
Enter your changes again in the updated window, or start the interrupted activity again.
In earlier versions of Dynamics 365, certain situations allowed code that an end-user runs to modify a record after a newer version of the record was written and committed to the database. This would overwrite the newer changes. However, in Dynamics 365 Business Central, we have restricted the MODIFY Method (RecordRef), RENAME Method (RecordRef), and DELETE Method (RECORDREF) so that the end-user receives the following run-time error in these certain situations:
Unable to change an earlier version of the <Table Name> record. The record should be read from the database again. This is a programming error.
You must design your application so that you use the most up-to-date version of the record for modifications to the database. You use the GET Method (RecordRef) to refresh the record with the latest version.
The following example deletes a record from the Customer table. The code starts by opening the Customer table (18) as a RecordRef variable that is named MyRecordRef. The FIELD Method (RecordRef) creates a FieldRef that is named MyFieldRef for field 1, which is the primary key of the Customer table. The VALUE Method (FieldRef, TestPage Field) assigns the value 10000 to the field that the MyFieldRef variable refers to. The FIND Method (RecordRef) searches the table for a record with field 1 = 10000. If the record is found, then it is deleted, the table is modified, and a message is displayed.
var varRecordToDelete: Code; MyRecordRef: RecordRef; Text000: Label 'Customer %1 is deleted.; begin varRecordToDelete := '10000'; MyRecordRef.OPEN(18); MyFieldRef := MyRecordRef.FIELD(1); MyFieldRef.VALUE := varRecordToDelete; if MyRecordRef.FIND('=') then begin ifMyRecordRef.DELETE then begin MyRecordRef.MODIFY; MESSAGE(Text000, MyFieldRef.VALUE); end; end; end;