Skip to content

Sending file via custom codeunit

In this documentation, we will create a sample codeunit that enables the integration of external code, allowing Document Dispatch to send files received via a codeunit.

Info

This custom codeunit must be implemented within a custom extension. Please consider the comments within the sample codeunit.

The following requirements must be met: The Document Dispatch extension must be licensed. The Document Dispatch extension must be referenced in the custom extension.

Document Dispatch Custom Codeunit:

AL
codeunit 50000 "Document Dispatch Custom Codeunit"
{
    TableNo = "SIM_EDS Record Attachment";
    Permissions = tabledata "SIM_EDS Queue Line" = RIM;
    trigger OnRun()
    begin
        CreateCustomAttatchments(Rec)
    end;

    var
        GlobalCodeunitSIMEDSSI: Codeunit "SIM_EDS SI";

    procedure CreateCustomAttatchments(ParamRecordSIMEDSRecordAttachment: Record "SIM_EDS Record Attachment")
    var
        LocalRecordSIMEDSQueueLine: Record "SIM_EDS Queue Line";
        LocalCodeunitBase64Convert: Codeunit "Base64 Convert";
        LocalCodeunitSIMCORERecordRef: Codeunit "SIM_CORE RecordRef";
        LocalCodeunitSIMCOREDataMapping: Codeunit "SIM_CORE Data Mapping";
        LocalCodeunitSIMCOREText: Codeunit "SIM_CORE Text";
        LocalRecordId: RecordId;
        LocalRRecordRef: RecordRef;
        LocalMappingRecordRef: RecordRef;
        LocalResultRecordRef: RecordRef;
        LocalOOutStream: OutStream;
        LocalFileBase64StringText: Text;
        LocalFilenameTempText: Text;
        LocalLineNoInteger: Integer;
        LocalDoExportBoolean: Boolean;

    begin
        // Check if the codeunit is set up
        if GlobalCodeunitSIMEDSSI.GetCodeunitRecID() = '' then exit;
        if GlobalCodeunitSIMEDSSI.GetCodeunitQueueID() = 0 then exit;
        if not Evaluate(LocalRecordId, GlobalCodeunitSIMEDSSI.GetCodeunitRecID()) then exit;
        if not LocalRRecordRef.Get(LocalRecordId) then exit;

        // Get the Queue Line No. from the Document Dispatch SI codeunit
        LocalLineNoInteger := GlobalCodeunitSIMEDSSI.GetCodeunitQueueLineNo();

        // Retrieve the record reference from the Attachment Record
        if ParamRecordSIMEDSRecordAttachment."Dynamic Mapping Code" = '' then
            LocalCodeunitSIMCORERecordRef.FilterRecRefByKey(LocalRRecordRef, LocalResultRecordRef)
        else begin
            LocalCodeunitSIMCORERecordRef.FilterRecRefByKey(LocalRRecordRef, LocalMappingRecordRef);
            LocalCodeunitSIMCOREDataMapping.GetRecordFromMappingHeader(
                ParamRecordSIMEDSRecordAttachment."Dynamic Mapping Code",
                LocalMappingRecordRef,
                LocalResultRecordRef
            );
            LocalMappingRecordRef.Close();
        end;

        // Iterate through the result records
        if LocalResultRecordRef.FindSet() then
            repeat
                LocalDoExportBoolean := false;

                // Logic to determine if the record should be exported
                LocalDoExportBoolean := true;

                if LocalDoExportBoolean then begin
                    LocalLineNoInteger += 10000;
                    LocalRecordSIMEDSQueueLine.Init();
                    LocalRecordSIMEDSQueueLine."Queue Entry No." := GlobalCodeunitSIMEDSSI.GetCodeunitQueueID();
                    LocalRecordSIMEDSQueueLine."Line No." := LocalLineNoInteger;

                    LocalRecordSIMEDSQueueLine.Type := ParamRecordSIMEDSRecordAttachment.Type;
                    LocalRecordSIMEDSQueueLine."Attachment Group" := ParamRecordSIMEDSRecordAttachment."Attachment Group";
                    LocalRecordSIMEDSQueueLine."Record Attachment Line No." := ParamRecordSIMEDSRecordAttachment."Line No.";
                    LocalRecordSIMEDSQueueLine.Description := ParamRecordSIMEDSRecordAttachment.Description;
                    LocalRecordSIMEDSQueueLine."Source ID" := LocalResultRecordRef.RecordId;

                    LocalRecordSIMEDSQueueLine."Codeunit File Found" := true;
                    LocalRecordSIMEDSQueueLine."Optional Attachment" := ParamRecordSIMEDSRecordAttachment."Optional Attachment";
                    LocalRecordSIMEDSQueueLine."Attach Tag" := ParamRecordSIMEDSRecordAttachment."Default selection";

                    case ParamRecordSIMEDSRecordAttachment."Codeunit PDF Type" of
                        //PDF
                        ParamRecordSIMEDSRecordAttachment."Codeunit PDF Type"::PDF,
                        ParamRecordSIMEDSRecordAttachment."Codeunit PDF Type"::"PDF/A-3":
                            begin
                                LocalFilenameTempText := 'Test.pdf';
                                // Convert file to Base64 and write to queue line
                                LocalFileBase64StringText := 'BASE64STRING';
                            end;

                        // Word
                        ParamRecordSIMEDSRecordAttachment."Codeunit PDF Type"::"Word":
                            begin
                                LocalFilenameTempText := 'Test.docx';
                                LocalFileBase64StringText := 'BASE64STRING';
                            end;

                        //Excel
                        ParamRecordSIMEDSRecordAttachment."Codeunit PDF Type"::"Excel":
                            begin
                                LocalFilenameTempText := 'Test.xlsx';
                                LocalFileBase64StringText := 'BASE64STRING';
                            end;

                        //XML
                        ParamRecordSIMEDSRecordAttachment."Codeunit PDF Type"::"XML":
                            begin
                                LocalFilenameTempText := 'Test.xml';
                                LocalFileBase64StringText := 'BASE64STRING';
                            end;
                    end;

                    LocalRecordSIMEDSQueueLine."Document Name" := CopyStr(LocalFilenameTempText, 1, 250);
                    LocalRecordSIMEDSQueueLine."Filename Extension" := CopyStr(
                                LocalCodeunitSIMCOREText.GetFileExtension(LocalRecordSIMEDSQueueLine."Document Name"),
                                1,
                                MaxStrLen(LocalRecordSIMEDSQueueLine."Filename Extension")
                            );

                    Clear(LocalOOutStream);
                    LocalRecordSIMEDSQueueLine."Document Blob".CreateOutStream(LocalOOutStream);
                    LocalCodeunitBase64Convert.FromBase64(LocalFileBase64StringText, LocalOOutStream);

                    // Insert the queue line
                    LocalRecordSIMEDSQueueLine.Insert();
                end;
            until LocalResultRecordRef.Next() = 0;

        GlobalCodeunitSIMEDSSI.SetCodeunitQueueLineNo(LocalLineNoInteger);
    end;
}