Skip to content

Extending Clever Document Delivery

Expressions

“Expression” refers to some custom code that will return a value that can be used as the value for a Document Type field. They are useful when you wish to assign a value to a Document Type field that is the result of some calculation and cannot be retrieved from either the Primary Table or a Linked Table.

For example, imagine you want a Document Type Field to hold the number of unpaid invoices so that it can be included in the body of the customer statement email. You want the statement email to include the phrase “You have [[OVERDUEINV]] overdue invoices.”

Search in the menu for the “Doc. Delivery Expressions” page. Create a new record with a unique number and a description.

Create a new Document Field called OVERDUEINV and add a Document Field Line of Source Type Expression and a Source Type No. to indicate which expression should be called to calculate its value.

The OnEvaluateExpression has the following signature:

OnEvaluateExpression(SourceTypeNo: Integer; PrimaryRef: RecordRef; var Result: Text; var Handled: Boolean)

  • SourceTypeNo is the expression no

  • PrimaryRef is a record ref to the record that has been submitted to Clever Document Delivery (which will be a customer record in our example)

  • Result is the result of the expression and should be set by an event subscription to be used as the value of the Document Field

  • The Handled flag should be set by our event subscription to tell Clever Document Delivery to use the value that our function has returned

Create an event subscription to determine the count of overdue invoices and set the Result of the expression.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"DD Evaluate Expression
CDDTMN", 'OnEvaluateExpression', '', false, false)]  
local procedure OnEvaluateExpression(PrimaryRef: RecordRef; SourceTypeNo:
Integer; var Result: Text; var Handled: Boolean)  
var  
    Customer: Record Customer;  
    CustLedgEntry: Record "Cust. Ledger Entry";  
begin  
    if Handled then  
        exit;

    case SourceTypeNo of  
        1:  
            begin  
                PrimaryRef.SetTable(Customer);  
                CustLedgEntry.SetRange("Customer No.", Customer."No.");  
                CustLedgEntry.SetRange(Open, true);  
                CustLedgEntry.SetRange("Document Type", CustLedgEntry."Document Type"::Invoice);  
                CustLedgEntry.SetFilter("Due Date",'..%1', Today() - 1);  
                Result := Format(CustLedgEntry.Count);  
                Handled := true;  
            end;  
    end;  
end;

OnAfterInsertAttachments

The OnAfterInsertAttachments event is called after the attachments from the Document Type card have been evaluated and added to the Document record for a given source record.

The event has the following signature:

OnAfterInsertAttachments(RecRef: RecordRef; var Document: Record "DD Document CDDTMN"; var TempAttachment: Record "Temp Attachment CDDTMN" temporary; var Handled: Boolean)

  • RecRef is a record ref of the document that has been submitted to Clever Document Delivery

  • Document is the Document Log record that has been created and holds all the settings required to send the document (sender, recipients, subject, body, attachments etc.)

  • LanguageCode is the Language Code that has been calculated for this document

  • TempAttachment is the temporary set of attachments that should be added to this document when it is sent

The TempAttachment record has an AddNewEntry method with two overloads to add new attachments to the set.

  • Name: the file name to give this attachment when it is attached to the email (this should include the necessary file extension)

  • TempBlob (codeunit): holds the file that should be attached to the document

  • FileProcessingType (enum): determines the file processing type that should be applied to this attachment. This should only be used with PDF attachments and the options are:

    • None – no processing

    • Merge – merge this PDF to the back of the previous attachment (as determined by the Document Type attachments)

    • Watermark – use this PDF to watermark the previous PDF attachment (as determined by the Document Type attachments)

Example

This method subscribes to the OnAfterInsertAttachments event and: - Asks the user to confirm if there are any additional attachments to add - Retrieves the file name from File Mgt. BLOBImport and populates TempBlob - Prompts the user for the file processing type to use - Calls AddNewEntry to add the attachment to the set to attach to the document

This code is intended as a sample only and not for a production environment. Replace the Confirm and StrMenu lines with your own business logic to determine which attachments to upload and how to process them.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Document Delivery Mgt. CDDTMN", 'OnAfterInsertAttachments', '', false, false)]
    local procedure OnAfterInsertAttachments(RecRef: RecordRef; var Document: Record "DD Document CDDTMN"; var TempAttachment: Record "Temp Attachment CDDTMN"; var Handled: Boolean)
    var
        FileMgt: Codeunit "File Management";
        TempBlob: Codeunit "Temp Blob";
        FileProcessingTypeSelection: Integer;
        FileName: Text;
    begin
        while Confirm(StrSubstNo('Upload additional attachments to %1', RecRef.RecordId())) do begin
            FileName := FileMgt.BLOBImport(TempBlob, '');
            FileProcessingTypeSelection := StrMenu('None,Merge,Watermark', 1, 'Please select a file processing type');            
            TempAttachment.AddNewEntry(FileName, TempBlob, FileProcessingTypeSelection - 1);
        end;
    end;