Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

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;
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 4,924 on at
  • gdrenteria Profile Picture
    gdrenteria 12,338 Most Valuable Professional on at
    Reverse a finished production order automatically with code
    Hi
    I would say that the question is duplicated.
    Best
    GR

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,409 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,479 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans