The AL developer preview is still evolving with frequent updates. Follow us on the Dynamics NAV Team Blog for the announcements.
INSERT Method (RecordRef)
Inserts a record into a table.
[Ok :=] RecordRef.INSERT([RunTrigger])
The RecordRef that refers to the record that you want to insert.
The record cannot be from table 2000000001, the Object table or table 2000000006, the Company table.
Specifies whether to run the AL code in the OnInsert Trigger.
If this parameter is true, the code on the OnInsert trigger will be executed.
If this parameter is false, the code on the OnInsert trigger will not be exectued.
Property Value/Return Value
true if the record was inserted; otherwise, false.
If you omit this optional return value and if the record cannot be inserted, a run-time error occurs. If you include the return value, you must handle any errors.
Records are uniquely identified by the values in primary key fields. The Database Management System (DBMS) checks the primary key for the table before it inserts a new record.
If the table contains an auto-increment field, the auto-increment feature is used if the record contains a zero value in that field. The auto-increment feature enters the new value into the field as part of the insert.
If the auto-increment field contains a non-zero value, that value is inserted into the table and the auto-increment feature is not used. If the value in the auto-increment field is greater than the last auto-increment value in the table, the next auto-increment value that is entered into the table will be greater than the value in the field that you just inserted. If the value in the auto-increment field already exists in the table, a run-time error occurs.
This method works the same as the INSERT Method (Record).
The following example opens a table 18 (Customer) with a RecordRef variable that is named CustomerRecref. The FIELD Method (RecordRef) creates a FieldRef variable that is named MyFieldRef for the field. The INIT Method (RecordRef) initializes the values in the fields by using default values and then the INSERT method inserts a new record. The new record is 1120. This is the primary key for the new record.
In this example, the INIT method is called before the primary key is assigned a value. The INIT method does not initialize primary key fields. Therefore calling the INIT Method (RecordRef) before or after you assign values to the primary key field does not make any difference.
This example requires that you create the following global variables and text constants.
|Text constant||ENU value|
|Text000||The value of the field after you insert the record is %1.|
CustomerRecref.OPEN(18); MyFieldRef := CustomerRecref.FIELD(1); CustomerRecref.INIT; MyFieldRef.VALUE := '1120'; CustomerRecref.INSERT; MESSAGE(‘%1’, MyFieldRef.VALUE);