ALAppExtensions icon indicating copy to clipboard operation
ALAppExtensions copied to clipboard

[Event Request] Table 77 "Report Selections", Functions SendToZipForVend,SendToZipForCust,SendToDiskForVend,SendToDiskForCust,SendEmailDirectly: OnBeforeExecuteReport

Open nd2403 opened this issue 5 years ago • 1 comments

Hello, we need to influence report selection before sending the document, thank you in advance.

local procedure SendEmailDirectly(ReportUsage: Enum "Report Selection Usage"; RecordVariant: Variant; DocNo: Code[20]; DocName: Text[150]; FoundBody: Boolean; FoundAttachment: Boolean; ServerEmailBodyFilePath: Text[250]; var DefaultEmailAddress: Text[250]; ShowDialog: Boolean; var TempAttachReportSelections: Record "Report Selections" temporary; var CustomReportSelection: Record "Custom Report Selection") AllEmailsWereSuccessful: Boolean
    var
        TempBlob: Codeunit "Temp Blob";
        DocumentMailing: Codeunit "Document-Mailing";
        OfficeAttachmentManager: Codeunit "Office Attachment Manager";
        DocumentRecord: RecordRef;
        EmailAddress: Text[250];
        IsHandled: Boolean;
        AttachmentStream: InStream;
    begin
        IsHandled := false;
        DocumentRecord.GetTable(RecordVariant);
        OnBeforeSendEmailDirectly(Rec, ReportUsage, RecordVariant, DocNo, DocName, FoundBody, FoundAttachment,    ServerEmailBodyFilePath, DefaultEmailAddress, ShowDialog, TempAttachReportSelections, CustomReportSelection, AllEmailsWereSuccessful, IsHandled);
        if IsHandled then
            exit(AllEmailsWereSuccessful);

        AllEmailsWereSuccessful := true;

        ShowNoBodyNoAttachmentError(ReportUsage, FoundBody, FoundAttachment);

        if FoundBody and not FoundAttachment then begin
            TempBlob.CreateInStream(AttachmentStream);
            EmailAddress := CopyStr(
                GetNextEmailAddressFromCustomReportSelection(CustomReportSelection, DefaultEmailAddress, Usage, Sequence), 1, MaxStrLen(EmailAddress));
            AllEmailsWereSuccessful := DocumentMailing.EmailFile(AttachmentStream, '', ServerEmailBodyFilePath, DocNo, EmailAddress, DocName, not ShowDialog, ReportUsage.AsInteger(), DocumentRecord);
        end;

        if FoundAttachment then begin
            if ReportUsage = Usage::JQ then begin
                Usage := ReportUsage;
                CustomReportSelection.SetFilter(Usage, GetFilter(Usage));
                if CustomReportSelection.FindFirst then
                    if CustomReportSelection.GetSendToEmail(true) <> '' then
                        DefaultEmailAddress := CustomReportSelection."Send To Email";
            end;

            OnSendEmailDirectlyOnBeforeSendFiles(
              ReportUsage.AsInteger(), RecordVariant, DefaultEmailAddress, TempAttachReportSelections, CustomReportSelection);
            with TempAttachReportSelections do begin
                OfficeAttachmentManager.IncrementCount(Count - 1);
                repeat
 // - new
                    OnBeforeExecuteReport(TempAttachReportSelections, RecordVariant);
 // + new
                    EmailAddress := CopyStr(
                        GetNextEmailAddressFromCustomReportSelection(CustomReportSelection, DefaultEmailAddress, Usage, Sequence),
                        1, MaxStrLen(EmailAddress));
                    SaveReportAsPDFInTempBlob(TempBlob, "Report ID", DocumentRecord, "Custom Report Layout Code", ReportUsage);
                    TempBlob.CreateInStream(AttachmentStream);

                    AllEmailsWereSuccessful :=
                        AllEmailsWereSuccessful and
                        DocumentMailing.EmailFile(
                            AttachmentStream, '', ServerEmailBodyFilePath,
                            DocNo, EmailAddress, DocName, not ShowDialog, ReportUsage.AsInteger(), DocumentRecord);
                until Next() = 0;
            end;
        end;

        OnAfterSendEmailDirectly(ReportUsage.AsInteger(), RecordVariant, AllEmailsWereSuccessful);
        exit(AllEmailsWereSuccessful);
    end;
 procedure SendToDiskForCust(ReportUsage: Enum "Report Selection Usage"; RecordVariant: Variant; DocNo: Code[20]; DocName: Text; CustNo: Code[20])
    var
        TempReportSelections: Record "Report Selections" temporary;
        ElectronicDocumentFormat: Record "Electronic Document Format";
        ServerAttachmentFilePath: Text[250];
        ClientAttachmentFileName: Text;
    begin
        OnBeforeSetReportLayout(RecordVariant, ReportUsage.AsInteger());
        FindReportUsageForCust(ReportUsage, CustNo, TempReportSelections);
        with TempReportSelections do
            repeat
// - new
                OnBeforeExecuteReport(TempReportSelections, RecordVariant);
// + new
                ServerAttachmentFilePath := SaveReportAsPDF("Report ID", RecordVariant, "Custom Report Layout Code", ReportUsage);
                ClientAttachmentFileName := ElectronicDocumentFormat.GetAttachmentFileName(DocNo, DocName, 'pdf');
                FileManagement.DownloadHandler(
                    ServerAttachmentFilePath, '', '', FileManagement.GetToFilterText('', ClientAttachmentFileName), ClientAttachmentFileName);
            until Next() = 0;
    end;
    procedure SendToDiskForVend(ReportUsage: Enum "Report Selection Usage"; RecordVariant: Variant; DocNo: Code[20]; DocName: Text; VendorNo: Code[20])
    var
        TempReportSelections: Record "Report Selections" temporary;
        ElectronicDocumentFormat: Record "Electronic Document Format";
        ServerAttachmentFilePath: Text[250];
        ClientAttachmentFileName: Text;
    begin
        OnBeforeSetReportLayout(RecordVariant, ReportUsage.AsInteger());
        FindReportUsageForVend(ReportUsage, VendorNo, TempReportSelections);
        with TempReportSelections do
            repeat
// - new
                OnBeforeExecuteReport(TempReportSelections, RecordVariant);
// + new
                ServerAttachmentFilePath := SaveReportAsPDF("Report ID", RecordVariant, "Custom Report Layout Code", ReportUsage);
                ClientAttachmentFileName := ElectronicDocumentFormat.GetAttachmentFileName(DocNo, DocName, 'pdf');
                FileManagement.DownloadHandler(
                    ServerAttachmentFilePath, '', '', FileManagement.GetToFilterText('', ClientAttachmentFileName), ClientAttachmentFileName);
            until Next() = 0;
    end;
 procedure SendToZipForCust(ReportUsage: Enum "Report Selection Usage"; RecordVariant: Variant; DocNo: Code[20]; CustNo: Code[20]; var DataCompression: Codeunit "Data Compression")
    var
        TempReportSelections: Record "Report Selections" temporary;
        ElectronicDocumentFormat: Record "Electronic Document Format";
        ServerAttachmentTempBlob: Codeunit "Temp Blob";
        ServerAttachmentInStream: InStream;
        ServerAttachmentFilePath: Text;
    begin
        OnBeforeSetReportLayout(RecordVariant, ReportUsage.AsInteger());
        FindReportUsageForCust(ReportUsage, CustNo, TempReportSelections);
        with TempReportSelections do
            repeat
// - new
                OnBeforeExecuteReport(TempReportSelections, RecordVariant);
// + new
                ServerAttachmentFilePath := SaveReportAsPDF("Report ID", RecordVariant, "Custom Report Layout Code", ReportUsage);
                FileManagement.BLOBImportFromServerFile(ServerAttachmentTempBlob, ServerAttachmentFilePath);
                ServerAttachmentTempBlob.CreateInStream(ServerAttachmentInStream);
                DataCompression.AddEntry(
                  ServerAttachmentInStream, ElectronicDocumentFormat.GetAttachmentFileName(DocNo, Format(Usage), 'pdf'));
            until Next() = 0;
    end;
procedure SendToZipForVend(ReportUsage: Enum "Report Selection Usage"; RecordVariant: Variant; DocNo: Code[20]; VendorNo: Code[20]; var DataCompression: Codeunit "Data Compression")
    var
        TempReportSelections: Record "Report Selections" temporary;
        ElectronicDocumentFormat: Record "Electronic Document Format";
        ServerAttachmentTempBlob: Codeunit "Temp Blob";
        ServerAttachmentInStream: InStream;
        ServerAttachmentFilePath: Text;
    begin
        OnBeforeSetReportLayout(RecordVariant, ReportUsage.AsInteger());
        FindReportUsageForVend(ReportUsage, VendorNo, TempReportSelections);
        with TempReportSelections do
            repeat
// - new
                OnBeforeExecuteReport(TempReportSelections, RecordVariant);
// + new
                ServerAttachmentFilePath := SaveReportAsPDF("Report ID", RecordVariant, "Custom Report Layout Code", ReportUsage);
                FileManagement.BLOBImportFromServerFile(ServerAttachmentTempBlob, ServerAttachmentFilePath);
                ServerAttachmentTempBlob.CreateInStream(ServerAttachmentInStream);
                DataCompression.AddEntry(
                    ServerAttachmentInStream, ElectronicDocumentFormat.GetAttachmentFileName(DocNo, Format(Usage), 'pdf'));
            until Next() = 0;
    end;
    [IntegrationEvent(false, false)]
    local procedure OnBeforeExecuteReport(var ReportSelections: Record "Report Selections" temporary; var RecordVariant: Variant)
    begin
    end;

nd2403 avatar Nov 17 '21 13:11 nd2403

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. Please do not reply to this, as we do not monitor closed issues. If you have follow-up questions or requests, please create a new issue where you reference this one.

JesperSchulz avatar Nov 25 '21 05:11 JesperSchulz