Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

External Document No Validation

(1) ShareShare
ReportReport
Posted on by 30
Hi,
I have added bit of code to validate the external document No on Sales Order. The current function detects if a duplicate External Doc number is entered on Sales Order and errors, user can’t continue with the order until a unique External Doc Number is entered.
I want to slightly amend this so only a warning message pops up if a user enters a duplicate External Doc Number, and they still can continue with the order. Can someone please help with this.
Here is My Code
[EventSubscriber(ObjectType::Table, Database::"Sales Header", 'OnAfterValidateEvent', 'External Document No.', false, false)]
    procedure CheckForDuplicatedExternalDocumentNo(var Rec: Record "Sales Header"; var xRec: Record "Sales Header"; CurrFieldNo: Integer)
    var
        SalesHeaderRec: Record "Sales Header";
        WKText001: Label 'External Document No. %1 already exist on %2 %3';
        WKText002: Label 'External Document No. %1 already exist on %2 %3';
        DocumentType: Label 'Sales %1';
        Archive: Label 'Archive';
        SalesDocument: Text;
    begin
        SalesHeaderRec.Reset();
        SalesHeaderRec.SetCurrentKey("External Document No.");
        SalesHeaderRec.SetRange(SalesHeaderRec."External Document No.", Rec."External Document No.");
        if SalesHeaderRec.Find('-') then begin
        if SalesHeaderRec."Sell-to Customer No." = Rec."Sell-to Customer No." then begin
            SalesDocument := StrSubstNo(DocumentType, SalesHeaderRec."Document Type");
            Error(WKText001, Rec."External Document No.", SalesDocument, SalesHeaderRec."No.");  // need to show a warning message instead of throwing an error 
        end;
     
    end;
  • Verified answer
    Saurav.Dhyani Profile Picture
    14,314 Super User 2025 Season 1 on at
    External Document No Validation
    Hi,
     
    Which localization you are using. I don't see code to check duplicate external document No. in Sales order.
     
    if it's a code that you have written and would like to modify, try with below code.
     
       [EventSubscriber(ObjectType::Table, Database::"Sales Header", 'OnAfterValidateEvent', 'External Document No.', false, false)]
        procedure CheckForDuplicatedExternalDocumentNo(var Rec: Record "Sales Header"; var xRec: Record "Sales Header"; CurrFieldNo: Integer)
        var
            SalesHeaderRec: Record "Sales Header";
            WKText001: Label 'External Document No. %1 already exist on %2 %3';
            WKText002: Label 'External Document No. %1 already exist on %2 %3. Would Suggest you Update External Document No.?';
            DocumentType: Label 'Sales %1';
            Archive: Label 'Archive';
            SalesDocument: Text;
        begin
            SalesHeaderRec.Reset();
            SalesHeaderRec.SetCurrentKey("External Document No.");
            SalesHeaderRec.SetRange(SalesHeaderRec."External Document No.", Rec."External Document No.");
            if SalesHeaderRec.Find('-') then begin
                SalesDocument := StrSubstNo(DocumentType, SalesHeaderRec."Document Type");
                if SalesHeaderRec."Sell-to Customer No." = Rec."Sell-to Customer No." then
                    if Confirm(StrSubstNo(WKText002, Rec."External Document No.", SalesDocument, SalesHeaderRec."No.")) then;
            end;
        end;
     
     
    Regards,
    Saurav Dhyani

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,157 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,930 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans