<< Click to Display Table of Contents >> Navigation: Zetadocs SDK Guide > Creating a Per Tenant Extension > Delivery Post and Send |
Send after the posting Example
The following example illustrates how to use the Zetadocs Server Send codeunit to achieve Post and Send via Zetadocs, in this case Sales invoices. The logic will require a Per Tenant Extension.
This code allows to trigger the Send functionality after a Post. Please note the EventSubscriberInstance set to Manual that will avoid triggering a send after every posting activity, even when Zetadocs is not required.
codeunit 50681 "EventSubscriberManual"
{
EventSubscriberInstance = Manual;
var
PostAndSendWatchers: List of [RecordId];
procedure AddToPostAndSendList(recordToAdd: RecordId)
begin
if not PostAndSendWatchers.Contains(recordToAdd) then PostAndSendWatchers.Add(recordToAdd);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Post and Send hook ///
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)]
local procedure OnAfterPostSalesDoc(var SalesHeader: Record "Sales Header"; var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line"; SalesShptHdrNo: Code[20]; RetRcpHdrNo: Code[20]; SalesInvHdrNo: Code[20]; SalesCrMemoHdrNo: Code[20]; CommitIsSuppressed: Boolean; InvtPickPutaway: Boolean)
var
postedSalesInvoices: Record "Sales Invoice Header";
ZdReportSelections: Record "Report Selections";
ZdServerSend: Codeunit "Zetadocs Server Send";
ZdCommon: Codeunit "Zetadocs Common";
ZdRecIdFromEvent: RecordId;
ZdRecRef: RecordRef;
ZdReportId: Integer;
errorString: Text;
begin
ZdRecIdFromEvent := SalesHeader.RecordId;
if (PostAndSendWatchers.Contains(ZdRecIdFromEvent)) then begin
if (postedSalesInvoices.get(SalesInvHdrNo)) then begin
ZdRecRef.GetTable(postedSalesInvoices);
ZdCommon.FindSelectionReportId(ZdReportSelections.Usage::"S.Invoice", ZdReportId);
ZdServerSend.SendViaZetadocs(ZdRecRef, ZdReportId, '', true);
end else begin
errorString := StrSubstNo('Could not find the record %1', Format(ZdRecIdFromEvent));
Error(errorString);
end;
end;
end;
}
This code allows to add the Post and Send button and ensure that only the required document gets sent via Zetadocs.
pageextension 50680 "Zetadocs Sales Invoice" extends "Sales Invoice List"
{
actions
{
addfirst("Zetadocs")
{
action(ZetadocsPostAndSend)
{
Caption = 'Post And Send';
Image = SendMail;
ToolTip = 'Post and Send via Zetadocs';
ApplicationArea = All;
trigger OnAction()
var
ZdEventSubscribers: Codeunit EventSubscriberManual;
begin
ZdEventSubscribers.AddToPostAndSendList(Rec.RecordId);
BINDSUBSCRIPTION(ZdEventSubscribers);
PostingProcess();
end;
}
}
}
This code mimics the Microsoft posting process and should be adapted to the system needs.
local procedure PostingProcess()
var
ZdNavigateAfterPost: Option "Posted Document","New Document","Do Nothing";
begin
//Copy of Microsoft code from Business Central 16. May differ for other versions.
PostDocument(CODEUNIT::"Sales-Post (Yes/No)", ZdNavigateAfterPost::"Do Nothing");
end;
local procedure PostDocument(PostingCodeunitID: Integer; Navigate: Option)
var
SalesHeader: Record "Sales Header";
SalesInvoiceHeader: Record "Sales Invoice Header";
ApplicationAreaMgmtFacade: Codeunit "Application Area Mgmt. Facade";
LinesInstructionMgt: Codeunit "Lines Instruction Mgt.";
OfficeMgt: Codeunit "Office Management";
InstructionMgt: Codeunit "Instruction Mgt.";
NavigateAfterPost: Option "Posted Document","New Document","Do Nothing";
PreAssignedNo: Code[20];
IsScheduledPosting: Boolean;
DocumentIsPosted: Boolean;
begin
if ApplicationAreaMgmtFacade.IsFoundationEnabled then
LinesInstructionMgt.SalesCheckAllLinesHaveQuantityAssigned(Rec);
PreAssignedNo := "No.";
SendToPosting(PostingCodeunitID);
IsScheduledPosting := "Job Queue Status" = "Job Queue Status"::"Scheduled for Posting";
DocumentIsPosted := (not SalesHeader.Get("Document Type", "No.")) or IsScheduledPosting;
ZetadocsOnPostOnAfterSetDocumentIsPosted(SalesHeader, IsScheduledPosting, DocumentIsPosted);
if IsScheduledPosting then
CurrPage.Close;
CurrPage.Update(false);
if PostingCodeunitID <> CODEUNIT::"Sales-Post (Yes/No)" then
exit;
if OfficeMgt.IsAvailable then begin
SalesInvoiceHeader.SetCurrentKey("Pre-Assigned No.");
SalesInvoiceHeader.SetRange("Pre-Assigned No.", PreAssignedNo);
if SalesInvoiceHeader.FindFirst then
PAGE.Run(PAGE::"Posted Sales Invoice", SalesInvoiceHeader);
end else
case Navigate of
NavigateAfterPost::"Posted Document":
if InstructionMgt.IsEnabled(InstructionMgt.ShowPostedConfirmationMessageCode) then
ShowPostedConfirmationMessage(PreAssignedNo);
NavigateAfterPost::"New Document":
if DocumentIsPosted then begin
SalesHeader.Init();
SalesHeader.Validate("Document Type", SalesHeader."Document Type"::Invoice);
ZetadocsOnPostOnBeforeSalesHeaderInsert(SalesHeader);
SalesHeader.Insert(true);
PAGE.Run(PAGE::"Sales Invoice", SalesHeader);
end;
end;
end;
local procedure ShowPostedConfirmationMessage(PreAssignedNo: Code[20])
var
SalesInvoiceHeader: Record "Sales Invoice Header";
InstructionMgt: Codeunit "Instruction Mgt.";
OpenPostedSalesInvQst: Label 'The invoice is posted as number %1 and moved to the Posted Sales Invoices window.\\Do you want to open the posted invoice?', Comment = '%1 = posted document number';
begin
SalesInvoiceHeader.SetCurrentKey("Pre-Assigned No.");
SalesInvoiceHeader.SetRange("Pre-Assigned No.", PreAssignedNo);
if SalesInvoiceHeader.FindFirst then
if InstructionMgt.ShowConfirm(StrSubstNo(OpenPostedSalesInvQst, SalesInvoiceHeader."No."),
InstructionMgt.ShowPostedConfirmationMessageCode)
then
PAGE.Run(PAGE::"Posted Sales Invoice", SalesInvoiceHeader);
end;
[IntegrationEvent(false, false)]
local procedure ZetadocsOnPostOnAfterSetDocumentIsPosted(SalesHeader: Record "Sales Header"; var IsScheduledPosting: Boolean; var DocumentIsPosted: Boolean)
begin
end;
[IntegrationEvent(false, false)]
local procedure ZetadocsOnPostOnBeforeSalesHeaderInsert(var SalesHeader: Record "Sales Header")
begin
end;
}
Limitations:
Note that