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...
Answered

Item Last Direct Cost - Reset to Zero

(7) ShareShare
ReportReport
Posted on by 683
We have a request that all new Purchase Orders have the Line Direct Unit Cost set to zero as they are being entered, making sure that the user populates the correct Unit Cost (and not just accept what is populated).  The majority of Purchase Orders are created from Sales Orders using the Create Purchase Orders function.
 
We are not using Purchase Prices on the Item Cards (nor wish to, as there are many vendors that would need to be added/updated per item).
 
I have tested setting an Item's Last Direct Cost to zero, then creating the Purchase Order and it seems to do what we want.  Problem is that the Last Direct Cost is getting updated from the posting of a Purchase Invoice (I believe).
 
I would like to reset the Last Direct Cost to zero after Purchase Invoices are posted.  What is the best way to go about this?  A trigger on the Item table?
I have the same question (0)
  • ME-31032107-0 Profile Picture
    683 on at
    I wrote this bit of code on the "Purch. Inv. Line" table which seems to work.  If anyone has any comments or suggestions if this is not the best approach (or has pitfalls I did not consider), please advise.  Thanks.
     
    tableextension 50106 PurchInvLineTableExt extends "Purch. Inv. Line"
    {
        trigger OnAfterInsert()
        var
            Item: Record Item;
        begin
            If Type = Rec.Type::Item then begin
                Item.Get(Rec."No.");
                if Item."Last Direct Cost" <> 0 then begin
                    Item."Last Direct Cost" := 0;
                    Item.Modify()
                end
            end;
        end;
    }
  • Suggested answer
    Jainam M. Kothari Profile Picture
    15,639 Super User 2025 Season 2 on at
    Hello,
     
    To ensure users manually enter the correct Unit Cost on Purchase Orders, especially since you're not using Purchase Prices on Item Cards, you can reset the Item's Last Direct Cost to zero after posting a Purchase Invoice using an AL event subscriber on the Purch.-Post codeunit.
     
    This approach automatically clears the cost for each item involved in the invoice, prompting users to input the Unit Cost during PO creation.
     
    However, it may affect reporting or analytics that rely on Last Direct Cost, so consider whether a validation-based approach or custom PO logic might better suit your needs.
  • Suggested answer
    RockwithNav Profile Picture
    8,637 Super User 2025 Season 2 on at
    I would recommend not modifying the Item Card, as the Last Direct Cost might be used elsewhere as well. Instead, consider adding a piece of code in the OnAfterValidate trigger of the Quantity field to set the cost to 0 directly on the Purchase Line.
  • ME-31032107-0 Profile Picture
    683 on at
     
    With the majority of the Purchase Orders being created from Sales Orders by way of the Create Purchase Documents function, this approach will not work.  I initially started coding on the Purchase Card page, but realized this would only affect manually created POs.
     
    The Last Direct Cost field on the Item Card is editable, so unless there is some other reason to not update it, I think I need to take this approach.
  • ME-31032107-0 Profile Picture
    683 on at
     
    Using your CodeUnit approach, I wrote this, which seems to work.  Let me know if you think I missed anything.
     
    codeunit 50101 ResetLastDirectCost
    {
        [EventSubscriber(ObjectType::Codeunit, Codeunit:: "Purch.-Post", 'OnAfterPostPurchLine','', false, false)]
        local procedure MyProcedure(PurchInvLine: Record "Purch. Inv. Line")
        var Item: Record Item;
        begin
            If PurchInvLine.Type = PurchInvLine.Type::Item then begin
                Item.Get(PurchInvLine."No.");
                if Item."Last Direct Cost" <> 0 then begin
                    Item."Last Direct Cost" := 0;
                    Item.Modify()
                end
            end;
        end;
    }
  • Suggested answer
    YUN ZHU Profile Picture
    95,729 Super User 2025 Season 2 on at
    Hi, Just adding some information and hopefully giving you some hints too. Please note that in addition to posting, some other operations will also update this field.
     
    Thanks
    ZHU
  • Suggested answer
    Mansi Soni Profile Picture
    8,909 Super User 2025 Season 2 on at
  • Suggested answer
    Sumit Singh Profile Picture
    10,273 on at
    ⚙️ Recommended Approach: Custom Codeunit + Event Subscriber
     
    Instead of writing a trigger directly on the `Item` table (which could get messy or block upgrades), **use an event subscriber on the `OnAfterPostPurchaseDocument` event**, then reset the `Last Direct Cost` programmatically.
     
    💡 Why this is safer than direct triggers:
    - Keeps core table logic untouched
    - Easier to upgrade and maintain
    - You can scope this logic narrowly (only on posted Purchase Invoices)
     
    ---
     
     🔧 Sample Logic Outline (AL Code)
     
    ```al
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch.-Post", 'OnAfterPostPurchaseDocument', '', false)]
    procedure ResetLastDirectCostAfterPO(var PurchHeader: Record "Purchase Header"; var GenJnlLine: Record "Gen. Journal Line")
    var
        PurchLine: Record "Purchase Line";
        ItemRec: Record Item;
    begin
        if PurchHeader."Document Type" = PurchHeader."Document Type"::Invoice then begin
            PurchLine.SetRange("Document No.", PurchHeader."No.");
            PurchLine.SetFilter(Type, '%1', PurchLine.Type::Item); // Only item lines
            if PurchLine.FindSet() then
                repeat
                    if ItemRec.Get(PurchLine."No.") then
                        ItemRec."Last Direct Cost" := 0;
                        ItemRec.Modify();
                until PurchLine.Next() = 0;
        end;
    end;
    ```
     
    You can also add filtering logic for item category or vendor if you want to apply this selectively.
     
    Please try this in Sandbox environment.
  • Verified answer
    ME-31032107-0 Profile Picture
    683 on at
    All,
     
    Thanks for your contributions & comments on this issue.  I would much rather be able to modify the process that creates the Purchase Order from the Sales Order to set the Purchase Line Unit Costs to zero (and also the Purchase Order totals), but I cannot figure out where that happens.  I have been reviewing the CodeUnit 1314 (Purch. Doc. From Sales Doc.), but I don't see where Unit Costs are set.
     
    I see another CodeUnit 7021 (Purchase Line - Price) which seems to set the PurchaseLine.Direct Unit Cost, but I am not sure when this is called.
     
    I also tried to update the Direct Unit Cost on the Page 1328 (Purch. Order From Sales Order), which is the worksheet used to create the PO.  I can update the Direct Unit Cost to zero after entering the Quantity to Purchase, but this does not work when I enter/change the Vendor.
     
    I can adopt the CodeUnit approaches suggested (which I have done successfully), but I am still not certain that updating the Item.DirectUnitCost is the best way to handle it (based on some of your comments).
     
    I get that setting the Item.DirectUnitCost to zero might not be the best approach, but without knowing how to modify the POs created from SOs, I'm not sure what other recourse I have.
  • Suggested answer
    YUN ZHU Profile Picture
    95,729 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 2,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans