From e526f859eeee0f182db81bb18ebb3917a02cc72b Mon Sep 17 00:00:00 2001 From: StefanMaron Date: Wed, 10 Jan 2024 21:51:22 +0100 Subject: [PATCH] First steps to get background sending on the way --- .../SalesDocumentReleased.Codeunit.al | 10 ++++--- .../src/foundation/BatchSendNtfys.Codeunit.al | 28 +++++++++++++++++++ .../src/foundation/NtfyEntry.Table.al | 10 +++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 MSDyn365BC.Ntfy/src/foundation/BatchSendNtfys.Codeunit.al diff --git a/MSDyn365BC.Ntfy/src/EventTypes/SalesDocumentReleased.Codeunit.al b/MSDyn365BC.Ntfy/src/EventTypes/SalesDocumentReleased.Codeunit.al index 38cc6f6..5ab81f5 100644 --- a/MSDyn365BC.Ntfy/src/EventTypes/SalesDocumentReleased.Codeunit.al +++ b/MSDyn365BC.Ntfy/src/EventTypes/SalesDocumentReleased.Codeunit.al @@ -30,6 +30,8 @@ codeunit 71179877 SalesDocumentReleasedNTSTM implements INtfyEventNTSTM Body: Codeunit "Http Content"; DoCall: Boolean; begin + IsolatedStorage.Set('NtfyDescription', StrSubstNo('Sales %1 - %2 - has been released', SalesHeader."Document Type", SalesHeader."No."), DataScope::User); + NtfyEntry.SetRange(EventType, NtfyEntry.EventType::SalesDocumentReleased); NtfyEntry.SetFilter(NtfyTopic, '<>%1', ''); if NtfyEntry.FindSet() then @@ -46,12 +48,12 @@ codeunit 71179877 SalesDocumentReleasedNTSTM implements INtfyEventNTSTM DoCall := not FilterSalesHeader.IsEmpty(); end; - if DoCall then begin - Body.Create(StrSubstNo('Sales %1 - %2 - has been released', SalesHeader."Document Type", SalesHeader."No.")); - RestClient.Post(StrSubstNo('https://ntfy.sh/%1', NtfyEntry.NtfyTopic), Body).GetContent().AsText(); - end; + if DoCall then + NtfyEntry.Mark(true); until NtfyEntry.Next() = 0; + NtfyEntry.MarkedOnly(true); + NtfyEntry.RunBatch(); end; } \ No newline at end of file diff --git a/MSDyn365BC.Ntfy/src/foundation/BatchSendNtfys.Codeunit.al b/MSDyn365BC.Ntfy/src/foundation/BatchSendNtfys.Codeunit.al new file mode 100644 index 0000000..726e839 --- /dev/null +++ b/MSDyn365BC.Ntfy/src/foundation/BatchSendNtfys.Codeunit.al @@ -0,0 +1,28 @@ +namespace StefanMaron.Ntfy; + +using System.RestClient; + +codeunit 71179879 BatchSendNtfysNTSTM +{ + TableNo = NtfyEntryNTSTM; + + trigger OnRun() + var + NtfyEntry: Record NtfyEntryNTSTM; + RestClient: Codeunit "Rest Client"; + Body: Codeunit "Http Content"; + Description: Text; + begin + IsolatedStorage.Get('NtfyDescription', DataScope::User, Description); + Body.Create(Description); + IsolatedStorage.Delete('NtfyDescription', DataScope::User); + + NtfyEntry.SetRange(EventType, NtfyEntry.EventType::SalesDocumentReopened); + NtfyEntry.SetFilter(NtfyTopic, '<>%1', ''); + if NtfyEntry.FindSet() then + repeat + Clear(RestClient); + RestClient.Post(StrSubstNo('https://ntfy.sh/%1', NtfyEntry.NtfyTopic), Body); + until NtfyEntry.Next() = 0; + end; +} \ No newline at end of file diff --git a/MSDyn365BC.Ntfy/src/foundation/NtfyEntry.Table.al b/MSDyn365BC.Ntfy/src/foundation/NtfyEntry.Table.al index 748fc97..c841931 100644 --- a/MSDyn365BC.Ntfy/src/foundation/NtfyEntry.Table.al +++ b/MSDyn365BC.Ntfy/src/foundation/NtfyEntry.Table.al @@ -47,4 +47,14 @@ table 71179875 NtfyEntryNTSTM INtfyEvent := Rec.EventType; INtfyEvent.SetFilters(Rec); end; + + procedure RunBatch() + var + TempSessionId: Integer; + begin + Commit(); + StartSession(TempSessionId, Codeunit::BatchSendNtfysNTSTM, CompanyName, Rec); + // if not TaskScheduler.CreateTask() then + // Codeunit.Run(Codeunit::BatchSendNtfysNTSTM, Rec); + end; } \ No newline at end of file