web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Adding a "Created by" field within purchase order

(0) ShareShare
ReportReport
Posted on by 10

I like to show "created by" and "last updated by" information on a purchase order. I believe that there is no built-in mechanism except customizing and extending Purchase Header. I appreciate if someone can provide any feedback on this. Thanks.

Categories:
I have the same question (0)
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    Yes, you need to:

    1) Create a tableextension object for Purchase Header and add the fields you need, with logic for inserting the value on that fields (OnInsert/OnModify trigger)

    2) Create a pageextension object and extends Purchase Order page for inserting that fields.

    Very simple to do.

  • Suggested answer
    mbharodia Profile Picture
    10 on at

    Okay. thanks.

  • mbharodia Profile Picture
    10 on at

    I like to update "last updated by" field I added on purchase header every time there is a new purchase line added or updated. Therefore, I extended "Purchase Line" table and added the following code for OnInsert and OnModify triggers.

     trigger OnInsert()
        begin
            SetPurchaseHeaderUpdateStatus("Document No.");
        end;
    
        trigger OnModify()
        begin
            SetPurchaseHeaderUpdateStatus("Document No.");
        end;
    
        local procedure SetPurchaseHeaderUpdateStatus(purchaseHeaderCode: Code[50])
        var
            purchaseHeader: Record "Purchase Header";
        begin        
            purchaseHeader.SetRange("No.", purchaseHeaderCode);
            purchaseHeader."Last Updated By" := UserId();
            purchaseHeader."Last Updated On" := CurrentDateTime();
            purchaseHeader.Modify();
        end;


    I receive the following error.

    The purchase header does not exist. Identification field and values: Document Type: 'quote'; No.=''

    During debug time I see that "purchaseHeader" is being assigned; code runs fine till I call purcahseHeader.Modify(). It looks like that it might be failing after Modify () method is called. Additionally, I am also wondering why the error message refers to the document type of quote when I am trying to fetch a purchase header.

    I really appreciate if someone can help to resolve this issue.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    The SetPurchaseHeaderUpdateStatus is triggered in OnInsert trigger, do PurchaseHeader record is not yet wrote to the database and the MODIFY instruction will fail.

  • mbharodia Profile Picture
    10 on at

    Okay. Thanks a lot for your reply. However, what's about OnModify() trigger? I believe that a header should be available within database during update of any purchase line. I tried with that too and I received the same error. Any suggestion … . Thanks.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    I think you can remove the SetPurchaseHeaderUpdateStatus("Document No.") call from OnInsert trigger and leave only the call from OnModify.

  • mbharodia Profile Picture
    10 on at

    Okay Thanks. I also needed to update the code. I needed to call FindFirst() after setting the filter range as shown below. I believe that my issue was related with not calling FindFirst().

    local procedure SetPurchaseHeaderUpdateStatus(purchaseHeaderCode: Code[50])
        var
            purchaseHeader: Record "Purchase Header";
        begin
            purchaseHeader.SetRange("No.", purchaseHeaderCode);
            if purchaseHeader.FindFirst() then begin
                purchaseHeader."Last Updated By" := UserId();
                purchaseHeader."Last Updated On" := CurrentDateTime();
                purchaseHeader.Modify();
            end;
    
        end;


    With the above changes code works for both: adding a new line and updating existing lines. I am assigning value to purchaseHaderCode from Rec."Document No." while calling the above method from the triggers.

    Again many thanks for your help and support.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans