Edit

Share via


RecordRef.Truncate([Boolean]) Method

Version: Available or changed with runtime version 16.0.

Deletes all records in a table that fall within a specified range, in an efficient maner. Keep in mind that Truncate allows for less concurrency than DeleteAll, as the entire table will be locked until the transaction is committed.

Syntax

[Ok := ]  RecordRef.Truncate([ResetAutoIncrement: Boolean])

Parameters

RecordRef
 Type: RecordRef
An instance of the RecordRef data type.

[Optional] ResetAutoIncrement
 Type: Boolean
Specifies if the AutoIncrement column should be reset to start from the initial value, or if it should stay at the current value. Tables without an AutoIncrement field ignore this parameter. The default is true.

Return Value

[Optional] Ok
 Type: Boolean
true if the table supports truncate; otherwise, false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.

Remarks

Truncate currently isn't supported in the following cases:

  • Temporary tables, system tables, and tables of type other than Normal.
  • When running within a try function.
  • Tables that have a security filter applied.
  • When the current filters contain flow fields, or use a high number of marked records.
  • When the OnAfter/OnBefore events are subscribed for the table.
  • Tables with media fields.

In this case, it's recommended to use DeleteAll instead. 

Example

The following example demonstrates how to truncate all records in a table and capture the optional return value to avoid a runtime error when truncate isn't supported.

procedure ExampleTruncate_RecordRef()
var
    RecRef: RecordRef;
    Ok: Boolean;
begin
    RecRef.Open(50101);
    Ok := RecRef.Truncate(true);
    RecRef.Close();
    if Ok then
        Message('RecordRef: Truncate succeeded and AutoIncrement reset.')
    else
        Message('RecordRef: Truncate not supported; consider DeleteAll.');
end;

The following example demonstrates how to truncate records matching a filter (in this case the table's "Location Code" field). This approach is only recommended when the majority of records match the filter because Truncate is most efficient in such scenarios.

procedure ExampleTruncateFiltered_RecordRef()
var
    RecRef: RecordRef;
    Ok: Boolean;
begin
    RecRef.Open(50101);
    // Field number 2 corresponds to Location Code in this table
    RecRef.Field(2).SetFilter('Red');
    Ok := RecRef.Truncate(false);
    RecRef.Close();
    if Ok then
        Message('RecordRef: Filtered truncate (Location=Red) succeeded (AutoIncrement preserved).')
    else
        Message('RecordRef: Filtered truncate not supported; consider deleting manually.');
end;

See Also

Insert, Modify, ModifyAll, Delete, DeleteAll, and Truncate methods Record.Truncate([Boolean]) method
RecordRef data type
Getting started with AL
Developing extensions