Dynamics 365 Business Central: How to check whether selected records have the same value in a field?

Dynamics 365 Business Central

Hi, Readers.
Yesterday a friend asked me a question, is it possible to check whether selected records have the same value in a field?
For example, on the Posted Sales Invoices page, users select several records and choose the new action to do some processing. If the customers of the selected invoices are all the same customer, the operation will be processed normally. If the customers of the selected invoice are not the same, and contains two or more, an error will be prompted. In other word, the customer must be same value on all lines you selected.
This is a very interesting question, but not very difficult. So in this post, I will share two easy ways to do it, hope this will help.

Test Video:

Method1:
Key Point
・SetFilter(“Sell-to Customer No.”, ‘<>%1’, “Sell-to Customer No.”);
・if not IsEmpty then
・Error

pageextension 50134 PostedSalesInvoices extends "Posted Sales Invoices"
{
    actions
    {
        addafter(Print)
        {
            action(IsSame)
            {
                Caption = 'Is Same?';
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                PromotedOnly = true;
                Image = Calculate;
                ApplicationArea = All;

                trigger OnAction()
                var
                    SalesInvoiceHeaderRec: Record "Sales Invoice Header";
                begin
                    CurrPage.SetSelectionFilter(SalesInvoiceHeaderRec);
                    if SalesInvoiceHeaderRec.FindSet() then
                        CheckDistinctCustomerNo(SalesInvoiceHeaderRec);
                end;
            }
        }
    }

    local procedure CheckDistinctCustomerNo(var SalesInvoiceHeader: Record "Sales Invoice Header")
    var
    begin
        SalesInvoiceHeader.SetFilter("Sell-to Customer No.", '<>%1', SalesInvoiceHeader."Sell-to Customer No.");
        if not SalesInvoiceHeader.IsEmpty then
            Error(DistinctCustomerErr)
        else
            Message('OK');
        SalesInvoiceHeader.SetRange("Sell-to Customer No.");
    end;

    var
        DistinctCustomerErr: Label 'The customer must be same value on all lines you selected.';
}


Method2:
Key Point
・Count Before SetRange
・Count After SetRange
・If CountBefore <> CountAfter then
・Error

pageextension 50134 PostedSalesInvoices extends "Posted Sales Invoices"
{
    actions
    {
        addafter(Print)
        {
            action(IsSame)
            {
                Caption = 'Is Same?';
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                PromotedOnly = true;
                Image = Calculate;
                ApplicationArea = All;

                trigger OnAction()
                var
                    SalesInvoiceHeaderRec: Record "Sales Invoice Header";
                begin
                    CurrPage.SetSelectionFilter(SalesInvoiceHeaderRec);
                    if SalesInvoiceHeaderRec.FindSet() then
                        CheckDistinctCustomerNo(SalesInvoiceHeaderRec);
                end;
            }
        }
    }

    local procedure CheckDistinctCustomerNo(var SalesInvoiceHeader: Record "Sales Invoice Header")
    var
        CountBefore: Integer;
        CountAfter: Integer;
    begin
        Clear(CountBefore);
        Clear(CountAfter);
        CountBefore := SalesInvoiceHeader.Count;
        SalesInvoiceHeader.SetRange("Sell-to Customer No.", SalesInvoiceHeader."Sell-to Customer No.");
        CountAfter := SalesInvoiceHeader.Count;
        if CountBefore <> CountAfter then
            Error(DistinctCustomerErr)
        else
            Message('OK');
        SalesInvoiceHeader.SetRange("Sell-to Customer No.");
    end;

    var
        DistinctCustomerErr: Label 'The customer must be same value on all lines you selected.';
}

END

Hope this will help you.

Thanks for reading.

ZHU

コメント

Copied title and URL