Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

External Document No Validation

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
    Saurav.Dhyani 17,965 Super User 2024 Season 2 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans