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";

    local 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";
        LocalRecordId: RecordId;
        LocalRRecordRef: RecordRef;
        LocalMappingRecordRef: RecordRef;
        LocalResultRecordRef: RecordRef;
        LocalOOutStream: OutStream;
        LocaliInteger: Integer;
        LocalFileBase64StringText: Text;
        LocalFilenameTempText: Text;
        LocalFileExtensionText: Text;
        LocalLineNoInteger: Integer;
        LocalExample1Boolean: Boolean;
        LocalExample2Boolean: Boolean;
        LocalDoExportBoolean: Boolean;
    begin
        // Check if the codeunit is setup
        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 EDS SI codeunit
        LocalLineNoInteger := GlobalCodeunitSIMEDSSI.GetCodeunitQueueLineNo();

        // Retrieve the recordref 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;

        // Repeat for each record found in the result recordref
        if LocalResultRecordRef.FindSet() then
            repeat
                // Set LocalDoExportBoolean to false so that the record is not exported by default
                LocalDoExportBoolean := false;

                // Perform checks to determine if the record should be exported
                if (NOT LocalExample1Boolean) AND LocalExample2Boolean then
                    LocalDoExportBoolean := true;

                // If the record should be exported, create a new queue line
                if LocalDoExportBoolean then begin
                    // LocalLineNoInteger needs to be incremented by 10000 for each new queue line
                    LocalLineNoInteger += 10000;

                    LocalRecordSIMEDSQueueLine.Init();
                    LocalRecordSIMEDSQueueLine."Queue Entry No." := GlobalCodeunitSIMEDSSI.GetCodeunitQueueID();
                    LocalRecordSIMEDSQueueLine."Line No." := LocalLineNoInteger;

                    // Set the document name and extension to the correct values
                    LocalFilenameTempText := 'test.pdf';
                    for LocaliInteger := StrLen(LocalFilenameTempText) downto 1 do
                        if LocalFilenameTempText[LocaliInteger] = '.' then begin
                            LocalFileExtensionText := CopyStr(LocalFilenameTempText, LocaliInteger, StrLen(LocalFilenameTempText));
                            LocaliInteger := 1;
                        end;

                    // Set the values of the queue line
                    LocalRecordSIMEDSQueueLine."Document Name" := CopyStr(LocalFilenameTempText, 1, 250);
                    LocalRecordSIMEDSQueueLine."Filename Extension" := CopyStr(LocalFileExtensionText, 1, MaxStrLen(LocalRecordSIMEDSQueueLine."Filename Extension"));
                    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;

                    case ParamRecordSIMEDSRecordAttachment."Default Output" of
                        ParamRecordSIMEDSRecordAttachment."Default Output"::" ":
                            LocalRecordSIMEDSQueueLine."Subtype" := LocalRecordSIMEDSQueueLine."Subtype"::" ";
                        ParamRecordSIMEDSRecordAttachment."Default Output"::Codeunit:
                            LocalRecordSIMEDSQueueLine."Subtype" := LocalRecordSIMEDSQueueLine."Subtype"::Codeunit;
                        ParamRecordSIMEDSRecordAttachment."Default Output"::Report:
                            LocalRecordSIMEDSQueueLine."Subtype" := LocalRecordSIMEDSQueueLine."Subtype"::Report;
                    end;

                    LocalRecordSIMEDSQueueLine."Codeunit File Found" := true;
                    LocalRecordSIMEDSQueueLine."User Change Report Settings" := ParamRecordSIMEDSRecordAttachment."User Change Report Settings";
                    LocalRecordSIMEDSQueueLine."Report Settings Needed" := ParamRecordSIMEDSRecordAttachment."Report Settings Needed";
                    LocalRecordSIMEDSQueueLine."Optional Attachment" := ParamRecordSIMEDSRecordAttachment."Optional Attachment";
                    LocalRecordSIMEDSQueueLine."Attach Tag" := ParamRecordSIMEDSRecordAttachment."Default selection";

                    // The LocalFileBase64StringText variable should be set to the base64 string of the file. The Logic how to get the base64 string is not provided in this snippet.
                    LocalFileBase64StringText := 'BASE64STRING';

                    // Write the base64 string to the queue line
                    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;
}