Jigsaw Blog

23

May
2011
0 comments

Unique External Document No. on Sales Order

In Dynamics NAV if you want to make sure the "External Document No." is unique for each Sell-to customer, use the following code on the OnValidate trigger of the field "External Document No." in the Sales Header table (36)

SalesHeader.RESET;

SalesHeader.SETRANGE(SalesHeader."External Document No.","External Document No.");

SalesHeader.SETRANGE(SalesHeader."Sell-to Customer No.","Sell-to Customer No.");

SalesHeader.SETFILTER(SalesHeader."No.",'<>%1', "No.");

IF SalesHeader.FINDFIRST AND ("External Document No." <> '') THEN

ERROR('The external document number should be unique! %2 already existent on %1', SalesHeader."No.","External Document No.");

SalesInvHeader.RESET;

SalesInvHeader.SETRANGE(SalesInvHeader."External Document No.","External Document No.");

SalesInvHeader.SETRANGE(SalesInvHeader."Sell-to Customer No.","Sell-to Customer No.");

SalesInvHeader.SETFILTER(SalesInvHeader."No.",'<>%1', "No.");

IF SalesInvHeader.FINDFIRST AND ("External Document No." <> '') THEN

ERROR('The external document number should be unique! %2 already existent on %1', SalesInvHeader."No.","External Document No.");

SalesCrMemoHeader.RESET;

SalesCrMemoHeader.SETRANGE(SalesCrMemoHeader."External Document No.","External Document No.");

SalesCrMemoHeader.SETRANGE(SalesCrMemoHeader."Sell-to Customer No.","Sell-to Customer No.");

SalesCrMemoHeader.SETFILTER(SalesCrMemoHeader."No.",'<>%1', "No.");

IF SalesCrMemoHeader.FINDFIRST AND ("External Document No." <> '') THEN

ERROR('The external document number should be unique! %2 already existent on %1',

SalesCrMemoHeader."No.","External Document No.");