Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Duplicate External Document # on SO

(1) ShareShare
ReportReport
Posted on by 253

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,

  • Duplicate External Document # on SO
    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
  • Suggested answer
    Damjan Zakojc Profile Picture
    Damjan Zakojc 431 on at
    RE: Duplicate External Document # on SO

    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

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 995 Super User 2024 Season 1 on at
    RE: Duplicate External Document # on SO

    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?

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans