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

Reverse a finished production order automatically with code

(1) ShareShare
ReportReport
Posted on by
I want to reverse a posted production order (and the consumption) automatically using code. I created this example code, but it doesn't work. : 
ProdOrderLine: Record "Prod. Order Line";
        ProdOrder: Record "Production Order";
        ItemJournalLine: Record "Item Journal Line";
        MfgSetup: Record "Manufacturing Setup";
        ForReservEntry_L: Record "Reservation Entry";
 
        ProductionJrnlMgt: Codeunit "Production Journal Mgt";
        ItemJnlPostBatch: Codeunit "Item Jnl.-Post Batch";
        CreateReservEntry_L: Codeunit "Create Reserv. Entry";
 
        ProgressBar: Dialog;
        GeneratingJnlLinesMsg: Label 'Generating journal lines for reversal.';
        Status_L: Enum "Reservation Status";
        ToTemplateName: Code[10];
        ToBatchName: Code[10];
 
        ErrorOrderLineNotExist: Label 'Prod. Order Line does not exist';
        ErrorOrderNotExist: Label 'Production Order does not exist';
    begin
        if not ProdOrderLine.Get(ProdOrderLine.Status::Released, ProdOrderNo_P, ProdOrderLines_P) then
            Error(ErrorOrderLineNotExist);
 
        if not ProdOrder.Get(ProdOrder.Status::Released, ProdOrderNo_P) then
            Error(ErrorOrderNotExist);
 
        MfgSetup.Get();
        ProductionJrnlMgt.SetTemplateAndBatchName();
        ProductionJrnlMgt.InitSetupValues();
 
        ProgressBar.Open(GeneratingJnlLinesMsg);
 
        ProductionJrnlMgt.DeleteJnlLines(ToTemplateName, ToBatchName, ProdOrder."No.", ProdOrderLines_P);
 
        ProductionJrnlMgt.CreateJnlLines(ProdOrder, ProdOrderLines_P);
 
        ItemJournalLine.Reset();
        if StrLen(UserId) > 10 then
            ItemJournalLine.SetRange("Journal Batch Name", CopyStr(UserId, 1, 10))
        else
            ItemJournalLine.SetRange("Journal Batch Name", UserId);
        ItemJournalLine.SetRange("Order Type", ItemJournalLine."Order Type"::Production);
        ItemJournalLine.SetRange("Order No.", ProdOrderNo_P);
        ItemJournalLine.SetRange("Order Line No.", ProdOrderLines_P);
 
        if ItemJournalLine.FindSet() then begin
            repeat
                // ItemJournalLine.Validate("Stop Time", 0);
                // ItemJournalLine.Validate("Setup Time", 0);
                // ItemJournalLine.Validate("Bin Code", BinCode);
                // ItemJournalLine.Modify();
                if (ItemJournalLine."Entry Type" = ItemJournalLine."Entry Type"::Output) then begin
                    ItemJournalLine.Validate(Quantity, -Qty_P);
                    ItemJournalLine.Modify();
                end else if (ItemJournalLine."Entry Type" = ItemJournalLine."Entry Type"::Consumption) then begin
                    ItemJournalLine.Validate("Entry Type", ItemJournalLine."Entry Type"::"Positive Adjmt.");
                    ItemJournalLine.Validate(Quantity, Qty_P);
                    ItemJournalLine.Modify();
                    CreateReservEntry_L.SetDates(0D, ForReservEntry_L."Expiration Date");
                    CreateReservEntry_L.CreateReservEntryFor(
                                  Database::"Item Journal Line", ItemJournalLine."Entry Type".AsInteger(),
                                  ItemJournalLine."Journal Template Name", ItemJournalLine."Journal Batch Name", 0, ItemJournalLine."Line No.", ItemJournalLine."Qty. per Unit of Measure",
                                  Qty_P, Qty_P * ItemJournalLine."Qty. per Unit of Measure", ForReservEntry_L);
                    CreateReservEntry_L.CreateEntry(
                   ItemJournalLine."Item No.", ItemJournalLine."Variant Code", ItemJournalLine."Location Code", '', 0D, 0D, 0, Status_L::Surplus);
                end;
 
            until ItemJournalLine.Next() = 0;
        end;
 
        ItemJnlPostBatch.Run(ItemJournalLine);
 
        ProgressBar.Close();
 
        exit('');
    end;
I have the same question (0)
  • Gerardo Rentería García Profile Picture
    25,169 Most Valuable Professional on at
    Hi
    I would say that the question is duplicated.
    Best
    GR
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,275 Super User 2025 Season 2 on at

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,143

#2
Jainam M. Kothari Profile Picture

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

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans