TryFunction database write is not rolled back
Properties
PC0040
Warning
Usage
Code Fix
Ignore Obsolete
Database changes made inside a [TryFunction] are not rolled back when an error is caught. On-premises, writes can also cause a runtime error when DisableWriteInsideTryFunctions is enabled.
PC0040 reports direct writes made by record built-in methods. A successful write can remain committed even if a later error causes the TryFunction to return false.
Example
[TryFunction]
local procedure TryInsertCustomer(var Customer: Record Customer)
begin
Customer.Insert(); // The insert is not rolled back if a later error is caught [PC0040]
Error('Something failed after the insert.');
end;Keep the TryFunction for validation, and perform the write after it succeeds:
procedure CreateCustomer(var Customer: Record Customer)
begin
if TryValidateCustomer(Customer) then
Customer.Insert();
end;
[TryFunction]
local procedure TryValidateCustomer(var Customer: Record Customer)
begin
Customer.TestField("No.");
Customer.TestField(Name);
end;When the diagnostic is reported
PC0040 is reported for calls to Insert, Modify, ModifyAll, Delete, DeleteAll, Rename, Truncate, AddLink, CopyLinks, DeleteLink or DeleteLinks inside a TryFunction when the receiver resolves to a non-temporary record and table. Calls with or without parentheses are recognized.
When the diagnostic is not reported
- Calls outside TryFunctions.
- Writes to temporary records, tables declared with
TableType = Temporary, or temporary page source tables. - Same-named methods on non-record types, such as
List.Insert,TextBuilder.InsertandHttpClient.Delete. RecordRefwrites, because whether aRecordRefis temporary is only known at runtime.- Calls to custom procedures. Enable PC0041 to review calls that may write without following method bodies.
See also
- Handling errors using try methods on Microsoft Learn
- PC0041 - Call may write to the database inside a TryFunction