bcquality/community/knowledge/performance/httpclient-inside-write-transaction-holds-locks.good.al
Stefano Demiliani 8373bc4717 fix(community/performance): correct httpclient and oncompanyopen good samples
httpclient-inside-write-transaction-holds-locks.good.al
- Pass Customer.RecordId as the last argument to CreateTask so the task
  is bound to the single customer that was written, not to all customers.
- Declare TableNo = Customer on the task codeunit so the platform loads
  the bound record into Rec automatically when OnRun executes.
- Replace the FindSet loop over all customers with Rec."No.", preserving
  the one-customer scope of the original SyncCustomerLastName procedure.

oncompanyopen-subscribers-must-not-do-io.good.al
- Replace Session.GetCurrentClientType() with Session.CurrentClientType(),
  the correct platform method name.
- Fix the idempotency check: TaskScheduler.TaskExists() requires a Guid,
  not a codeunit integer ID. Store the Guid returned by CreateTask in
  IsolatedStorage (DataScope::Company) under a fixed key; on the next
  login read it back as Text, Evaluate it to Guid, and pass that Guid to
  TaskExists so the type matches the method signature.
2026-08-21 14:42:47 +02:00

25 lines
925 B
AL

codeunit 50100 "HttpClient Holds Locks Good"
{
// Write completes inside the caller's transaction; HTTP deferred so locks are released with it.
procedure SyncCustomerLastName(var Customer: Record Customer)
begin
Customer."Search Name" := Customer.Name;
Customer.Modify(false);
// RecordId binds the task to this specific customer; the platform loads it into Rec on OnRun.
TaskScheduler.CreateTask(Codeunit::"Customer Sync Task", 0, true, CompanyName(), CurrentDateTime(), Customer.RecordId);
end;
}
codeunit 50101 "Customer Sync Task"
{
TableNo = Customer;
trigger OnRun()
var
Client: HttpClient;
Response: HttpResponseMessage;
begin
// Separate session: Rec is the single customer passed via RecordId; no write-transaction lock is held.
Client.Get(StrSubstNo('https://example.local/sync/%1', Rec."No."), Response);
end;
}