Dynamics 365 Business Central: Copy Comments from Customer to Sales Document – Customization

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to copy Comments from Customer to Sales Document.

As you might know, you can add extra information to G/L accounts, customers cards, or sales orders to communicate exceptions or special agreements to other users. Practically all cards and document have a Comments action, which opens the Comment Sheet page where you can write or read comments.

On documents, you can also add comments to individual lines.

Comments on ongoing documents are transferred to the related posted document. For example, a comment on a sales order is transferred to a resulting posted sales shipment. In addition, you can specify if you want comments to be transferred from one type of document to another resulting type of document, such as from a sales order to a sales invoice. You do this in the Sales & Receivables and the Purchases & Payables pages respectively.

Well, this is a very useful feature. But I saw a question in the Business Central forum a few days ago, “Is there anyway automatically get comments from customer card to sales order?”, more details: Customer card comments to sales order (dynamics.com)

Yes, as of now, this function is not included in the standard features……So we can only customize it.
Let’s see more details. First of all, the table used in the customer card and the table used in the sales document are not the same table.

table 97 “Comment Line”: Customer Card

table 44 “Sales Comment Line”: Sales Order

We can look at the standard processing of Sales Document.
codeunit 80 “Sales-Post” -> local procedure InsertInvoiceHeader

Same table (table 44 “Sales Comment Line”):

Let’s start.
As standard, create a new field in Sales & Receivables to manage this functionality.

Then insert the logic to trigger OnAfterInsert() and trigger OnAfterValidate() of “Sell-to Customer No.”.

Simple test:

Great.

Test video: Youtube

Business Central short video: Copy Comments from Customer to Sales Document – Customization

PS:
1. “Sales Comment Document Type” has the same order as “Sales Document Type”.
enum 44 “Sales Comment Document Type”

enum 36 “Sales Document Type”

2. You can also put this processing in other events or triggers, but please note that because Customer No. is not the primary key of Sales Header, the system can create a Sales Document without Customer No., so do not only put it in the OnInsert trigger of the table.

3. Vendor and Purchase Document can also be customized in a similar way.

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

tableextension 50200 SalesReceivablesSetupExt extends "Sales & Receivables Setup"
{
    fields
    {
        field(50200; "Copy Cust Comments to Order"; Boolean)
        {
            Caption = 'Copy Comments from Customer to Order';
            InitValue = true;
        }
    }
}

pageextension 50200 SalesReceivablesSetupExt extends "Sales & Receivables Setup"
{
    layout
    {
        addbefore("Copy Comments Order to Invoice")
        {
            field("Copy Cust Comments to Order"; Rec."Copy Cust Comments to Order")
            {
                ApplicationArea = All;
                ToolTip = 'If this field is checked, the comments from the customer card will be copied to the sales order header.';
            }
        }
    }
}

tableextension 50201 SalesHeaderExt extends "Sales Header"
{
    fields
    {
        modify("Sell-to Customer No.")
        {
            trigger OnAfterValidate()
            begin
                if SalesReceivablesSetup.Get() then
                    if SalesReceivablesSetup."Copy Cust Comments to Order" then
                        CopyCommentsFromCustToSalesHeader();
            end;
        }
    }
    trigger OnAfterInsert()
    begin
        if SalesReceivablesSetup.Get() then
            if SalesReceivablesSetup."Copy Cust Comments to Order" then
                CopyCommentsFromCustToSalesHeader();
    end;

    var
        SalesReceivablesSetup: Record "Sales & Receivables Setup";

    local procedure CopyCommentsFromCustToSalesHeader()
    var
        CommentLine: Record "Comment Line";
        SalesCommentLine: Record "Sales Comment Line";
    begin
        if Rec."Sell-to Customer No." = '' then
            exit;
        SalesCommentLine.Reset();
        SalesCommentLine.SetRange("Document Type", Rec."Document Type");
        SalesCommentLine.SetRange("No.", Rec."No.");
        if SalesCommentLine.IsEmpty() then begin
            CommentLine.Reset();
            CommentLine.SetRange("Table Name", Enum::"Comment Line Table Name"::Customer);
            CommentLine.SetRange("No.", Rec."Sell-to Customer No.");
            if CommentLine.FindSet() then
                repeat
                    SalesCommentLine.Init();
                    SalesCommentLine."Document Type" := Rec."Document Type";
                    SalesCommentLine."No." := Rec."No.";
                    SalesCommentLine."Line No." := CommentLine."Line No.";
                    SalesCommentLine.Date := CommentLine.Date;
                    SalesCommentLine.Comment := CommentLine.Comment;
                    SalesCommentLine.Insert();
                until CommentLine.Next() = 0;
        end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL