Skip to content

Source-code based storage of documents

Document Central can be extended with code that allows uploading documents from a BLOB to Document Central via code. This approach makes it possible to automatically generate and add a Document Central barcode when a report is printed or when a document is posted or converted.

The following requirements must be met:

  • The Document Central module must be licensed.
  • CORE must be available in the extension as a reference.
  • Document Central must be present in the extension as a reference.

Adding Inbound Documents

For adding documents into the Inbound list 2 Functions in the "SIM_DI Upload Mgt" can be used AddInboundDocumentFromExternal & AddInboundDocumentFromSilentUpload.

When to use which function

Scenario Function to use
A document is automatically generated and uploaded during a BC process (e.g. report print, document posting, document conversion) AddInboundDocumentFromSilentUpload
The upload is triggered from an external system or integration (e.g. via API, from another extension, from Document Management) AddInboundDocumentFromExternal
The target record may not yet be committed to the database at upload time (e.g. during OnAfterInsert, during posting) AddInboundDocumentFromSilentUpload
The target record is already fully committed and the RecordId is known AddInboundDocumentFromExternal
Field mapping / assignment metadata needs to be passed as JSON AddInboundDocumentFromExternal
A specific Document Library must be set for the uploaded document AddInboundDocumentFromExternal
The initial status (Open / Pending / Archived) must be controlled explicitly AddInboundDocumentFromSilentUpload
The user or process that triggered the upload should be tracked (Origin) AddInboundDocumentFromSilentUpload

Summary: Use AddInboundDocumentFromSilentUpload for automated background uploads that originate from within Business Central (e.g. triggered by posting or printing). Use AddInboundDocumentFromExternal when integrating from outside BC or when metadata-driven assignment or a Document Library code is required.


AddInboundDocumentFromSilentUpload

Var Name Data Type Sub Type Length Description
No Filename Text The original file name of the document
No Base64File Text The file as a base64 string
No InboundListCode Code 20 Code of the Inboundlist
No StatusCheckText Text Statuscheck
No Recordref RecordRef The data record reference to which the document must be uploaded.
No ParamContentTypeText Text The Text of the Contenttype
No InfoTagText Text Infotag
No EnumSIMDIInboundDocSource Enum "SIM_DI Inbound Doc Source" The Source Status
No OriginText Text OriginText
Show example code
AL
var
    SalesHeader: Record "Sales Header";
    UploadMgt: Codeunit "SIM_DI Upload Mgt";
    FileManagement: Codeunit "File Management";
    Base64Convert: Codeunit "Base64 Convert";
    TempBlob: Codeunit "Temp Blob";
    RecordReference: RecordRef;
    InStr: InStream;
    Filename: Text;
    Base64File: Text;
    AllFilesFilterTxt: Label '*.*', Locked = true;
    AllFilesDescriptionTxt: Label 'All Files (*.*)|*.*', Comment = '{Split=r''\|''}{Locked=s''1''}';
    DialogCaptionTxt: Label 'Choose Upload File';
begin
    Filename := FileManagement.BLOBImportWithFilter(TempBlob, DialogCaptionTxt, '', AllFilesDescriptionTxt, AllFilesFilterTxt);
    Filename := FileManagement.GetFileName(Filename);
    if Filename = '' then exit;

    Clear(InStr);
    TempBlob.CreateInStream(InStr);
    Base64File := Base64Convert.ToBase64(InStr);

    SalesHeader.Reset();
    SalesHeader.FindFirst();
    RecordReference.GetTable(SalesHeader);

    UploadMgt.AddInboundDocumentFromSilentUpload(
        Filename,                                       // ParamFileNameText
        Base64File,                                     // ParamBase64Text
        'INBOUND-LIST',                                 // ParamInboundListCode
        '',                                             // ParamStatusCheckText
        RecordReference,                                // ParamRecordRef
        'INVOICE',                                      // ParamContentTypeText
        '',                                             // ParamInfoTagText
        Enum::"SIM_DI Inbound Doc Source"::Upload,      // ParamEnumSIMDIInboundDocSource
        UserId()                                        // ParamOriginText
    );
end;

AddInboundDocumentFromExternal

Var Name Data Type Sub Type Length Description
No ParamFileNameText Text The original file name of the document
No ParamBase64Text Text The file as a base64 string
No ParamInboundListCode Code 20 Code of the Inbound list to which the Document should be inserted
No ParamMetadataJsonText Text Metadata as a JSON string
No ParamRecordId RecordId The record ID to which the document must be linked
No ParamContentTypeText Text The text of the content type
No ParamDocumentLibraryCode Code 20 Code of the Document Library
No ParamInfoTagText Text An Info tag
No ParamEnumSIMDIInboundDocSource Enum "SIM_DI Inbound Doc Source" The source of the Document
Show example code
AL
var
    SalesHeader: Record "Purchase Header";
    UploadMgt: Codeunit "SIM_DI Upload Mgt";
    FileManagement: Codeunit "File Management";
    Base64Convert: Codeunit "Base64 Convert";
    TempBlob: Codeunit "Temp Blob";
    InStr: InStream;
    Filename: Text;
    Base64File: Text;
    MetadataJsonText: Text;
    MetadataJsonObject: JsonObject;
    AllFilesFilterTxt: Label '*.*', Locked = true;
    AllFilesDescriptionTxt: Label 'All Files (*.*)|*.*', Comment = '{Split=r''\|''}{Locked=s''1''}';
    DialogCaptionTxt: Label 'Choose Upload File';
begin
    Filename := FileManagement.BLOBImportWithFilter(TempBlob, DialogCaptionTxt, '', AllFilesDescriptionTxt, AllFilesFilterTxt);
    Filename := FileManagement.GetFileName(Filename);
    if Filename = '' then exit;

    Clear(InStr);
    TempBlob.CreateInStream(InStr);
    Base64File := Base64Convert.ToBase64(InStr);

    MetadataJsonObject.Add('dmssalesinvoiceno', 'PI-10000');
    MetadataJsonObject.Add('dmscustomerno', 'Adatum Corporation');
    MetadataJsonText := Format(MetadataJsonObject); // Format or other Extraction Methods

    SalesHeader.Get(SalesHeader."Document Type"::Invoice, 'PI-10000');

    UploadMgt.AddInboundDocumentFromExternal(
        Filename,                                       // ParamFileNameText
        Base64File,                                     // ParamBase64Text
        'INBOUND-LIST',                                 // ParamInboundListCode
        MetadataJsonText,                               // ParamMetadataJsonText - Can also be an Empty Text
        SalesHeader.RecordId(),                         // ParamRecordId - Necessary if the Assignment Method is Record Id
        'INVOICE',                                      // ParamContentTypeText
        'DOC-LIB-SALESINV',                             // ParamDocumentLibraryCode
        '',                                             // ParamInfoTagText
        Enum::"SIM_DI Inbound Doc Source"::Upload       // ParamEnumSIMDIInboundDocSource
    );
end;