Add, modify, or delete data with AL
Frequently, developers are asked to modify data in the database by using AL code.
The following statements allow modification of data:
Insert
Modify and ModifyAll
Delete and DeleteAll
Insert statement
The Insert statement is used to add new records to the database.
Before you can insert data, you need to set the values for each field
that you want to store. The Insert command, in addition to the modify and
delete commands, accept the RunTrigger Boolean parameter. By default,
the parameter value is false.
If the parameter is false, the Insert command doesn't run the
OnInsert trigger on the table level. You need to set the
value to true if you want the OnInsert trigger to be run.
This action is included for performance reasons.
This process is consistent with the assignment of a value to a field because it
likewise doesn't run the OnValidate trigger of the field.
var
customer: Record Customer;
begin
customer.Init();
customer."No." := '4711';
customer.Name := 'John Doe';
customer.Insert(true);
Though the OnInsert trigger isn't run by default, the
OnBeforeInsert or OnAfterInsert events are always run.
Modify and ModifyAll statements
To change values of an existing record, you first need to retrieve the
record. Then, you can update the values and use the Modify function
to store the changes in the database.
customer.Get('4711');
customer.Name := 'Richard Roe';
customer.Modify();
Similar to the Insert function, you need to provide a true value
to the Modify function to run the OnModify trigger on table
level.
If you want to update multiple records simultaneously, you can use the
ModifyAll function. The ModifyAll function accepts the
RunTrigger parameter as the third parameter of the function.
ModifyAll(Field, NewValue, [RunTrigger]
In the following example, all records where the Salesperson Code field equals (PS) are retrieved first, and then all the retrieved records are updated with the value (JR).
customer.SetRange("Salesperson Code", 'PS');
customer.ModifyAll("Salesperson Code", 'JR');
The Modify and ModifyAll functions don't
ask you for confirmation; the modification is run without
warning.
Delete and DeleteAll statements
As with the Modify function, you need to retrieve a record
from the database before you can delete a record. The Delete
and DeleteAll functions accept the RunTrigger parameter.
customer.Get('4711');
customer.Delete(true);
The DeleteAll function removes multiple records concurrently.
customer.SetRange("Salesperson Code", 'PS');
customer.DeleteAll();
The Delete and DeleteAll functions
don't ask you for confirmation; the deletion is completed
without warning.