web
You’re offline. This is a read only version of the page.
close
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...
Suggested Answer

Duplicate External Document # on SO

(1) ShareShare
ReportReport
Posted on by 275

Hey,

I am having issues with our invoicers creating duplicate external document numbers in the header on SOs, is there an out-of-the-box way to prevent this from happening?

Thanks,

I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    This does not sound like standard behavior. Are the sales orders imported into your system and do you have any customizations done to your system?

  • Suggested answer
    Damjan Zakojc Profile Picture
    439 on at

    There is no standard (out-of-the-box) function to check if the value entered in the field "External Document No." is unique.

    https://learn.microsoft.com/en-us/dynamics365/business-central/across-enter-external-document-numbers

    Customization is needed for this, which is pretty simple to create.
    I would create a global procedure to be called onAferValidateEvent for field "External Document No." on any business document you need to check for duplicated value (i.e. Sales Header for Sales Quotes, Sales Orders, Sales Header Archive for Archived Sales Quotes and Orders, etc.). Procedure should check if any duplicated value exists in T36 - "Sales Header", same would then needed to be done for T5107 - "Sales Header Archive", etc.

    Code sample below

    [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";
            SalesHeaderArchiveRec: Record "Sales Header Archive";
            SalesCrMemoArchiveRec: Record "Sales Cr.Memo 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
                SalesDocument := StrSubstNo(DocumentType, SalesHeaderRec."Document Type");
                Error(WKText001, Rec."External Document No.", SalesDocument, SalesHeaderRec."No.");
            end;
    
            SalesHeaderArchiveRec.Reset();
            SalesHeaderArchiveRec.SetCurrentKey("External Document No.");
            SalesHeaderArchiveRec.SetRange(SalesHeaderArchiveRec."External Document No.", Rec."External Document No.");
            if SalesHeaderArchiveRec.Find('-') then begin
                SalesDocument := StrSubstNo(DocumentType, SalesHeaderArchiveRec."Document Type");
                SalesDocument := Format(SalesDocument   ' '   Archive);
                Error(WKText002, Rec."External Document No.", SalesDocument, SalesHeaderArchiveRec."No.");
            end;
    
            SalesCrMemoArchiveRec.Reset();
            SalesCrMemoArchiveRec.SetCurrentKey("External Document No.");
            SalesCrMemoArchiveRec.SetRange(SalesCrMemoArchiveRec."External Document No.", Rec."External Document No.");
            if SalesCrMemoArchiveRec.FindFirst() then begin
                SalesDocument := StrSubstNo(DocumentType, SalesHeaderArchiveRec."Document Type");
                SalesDocument := Format(SalesDocument   ' '   Archive);
                Error(WKText002, Rec."External Document No.", SalesDocument, SalesCrMemoArchiveRec."No.");
            end;
        end;


    Test case
    1. insert new Sales Order
    2. Position in field: "External Document No." and insert value 000 -> control checks if any Sales Order or Sales Quote or archived Sales Order or archived Sales Quote with same External Document No. already exists and returns an error with the number of the Sales document.
    pastedimage1673642003162v1.png
    3. User has to insert unique value in the field: "External Document No." in order to successfully validate the field. User inserted value 0001 -> value is unique thus field is successfully validated.
    pastedimage1673642132714v2.png

    Please accept the answer it it resolves your issue.

    Thank you

    BR Damjan

  • Argirios Karmiris Profile Picture
    18 on at
    Hi Damjan,
     
    I'd just like to confirm that your method did work for us, who had a similar requirement.
    For us, we also need to check the External DOcument No. uniqueness against a specific sell-to customer, so I just added a 2nd if statement to test against the sell-to customer as well.
     
     
    Thanks for sharing the solution!
     
    Cheers,
    Argirios

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans