Check Order Date vs Posting Date on Warehouse Shipment

Hi Everybody,

I write code to check Order Date on table Sales Header and Posting Date on Table Warehouse Shipment Header.

When these two days are different will be warnings. But code not work, I’m using version NAV 2013

Please fix help me. Thanks very much.


SalesHeader.RESET;
WarehouseLine.RESET;
WarehouseLine.SETRANGE(WarehouseLine."Source No.",SalesHeader."No.");
IF (WarehouseLine.FINDSET AND SalesHeader.FINDSET) THEN BEGIN
  REPEAT
    OrderDate:=SalesHeader."Order Date";
    IF "Posting Date" <> OrderDate THEN
      BEGIN
        ERROR('Order Date <> Shipment Date')
      END;
  UNTIL (WarehouseLine.NEXT = 0) AND (SalesHeader.NEXT=0);
END;

Hi Dinhson,

First time I’ve seen this one. You can’t have two repeat loops in one like this. If you want to loop them, then you can do it inside with REPEAT a.findset REPEAT b.findset do-something; until b.next=0; until a.next=0; but that always ends up giving less readable code. And that’s not the only problem. So rather than trying to tell you what was wrong, then I would do it like this:


LOCAL PROCEDURE CheckSalesOrdersWarehousePostingDate()
// Local vars: SalesHeader
WITH SalesHeader DO BEGIN
  SETRANGE("Document Type","Document Type"::Order);
  IF SalesHeader.ISEMPTY THEN EXIT;

  REPEAT
    CheckWarehousePostingDate(SalesHeader);
  UNTIL WarehouseLine.NEXT = 0;
END;

LOCAL PROCEDURE CheckWarehousePostingDate(SalesHeader : Record "Sales Header")
// Local vars: WarehouseLine
WITH WarehouseShipmentLine DO BEGIN
  SETRANGE("Source Type", SalesHeader."Document Type");
  SETRANGE("Source No.",SalesHeader."No.");
  IF ISEMPTY THEN EXIT;
  REPEAT
    IF "Shipment Date" <> SalesHeader."Order Date" THEN
      ERROR('Order Date <> Shipment Date')
  UNTIL NEXT = 0;
END;

Two separate functions, with each their task. WITH a DO BEGIN gives you a much cleaner code. Put the functions in a codeunit, not directly in a page or table. I have seen “issues” when used there, due to the global Rec var that may conflict with your with statement. Also no need for RESET’s inside a function, as long as the record is only defined as a local var. Same thing can happen if you have both a global var and a local var with the same name.

Thanks you Erik. It’s work