Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Modify base application codeunit or create extension to add on to it

Posted on by 75

Hello, I've created an extension that adds an additional field in the Sales Order table & page (called Document Revision Number). But we have noticed that when we archive the sales order, it moves to a new table (Sales Order Archive) which does not have that field, so the information is lost. I need to figure out how to have it keep that field when it moves to the archive. The two things I need do are:

  1. Create the same field in the Sales Order Archive table
  2. Figure out how to have the codeunit that archives a Sales Order also archive my custom field. I assume I cannot directly edit the codeunit, but I need to create an extension to add on to it.

The problem is, the only thing I've found online is about events. I haven't been able to figure out how to create an event subscriber that would accomplish this. Any advice is appreciated.

Categories:
  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,025 Moderator on at
    RE: Modify base application codeunit or create extension to add on to it

    For the all old records which dont have that field you can go with Vaishnavi suggestion but for every new records you can create the same field with same ID in Sales header and Sales Archive Header table. so that I will transferred automatically when you will do the archive.

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 70,124 Super User 2024 Season 2 on at
    RE: Modify base application codeunit or create extension to add on to it

    Hi, Vaishnavi gave good examples, you can also look for clues yourself in codeunit 5063 ArchiveManagement.

    For example, this is the event used by Vaishnavi.

    pastedimage1657843456615v1.png

    Also, although I haven't tried it, the event below should work as well.

    pastedimage1657843582850v2.png

    Hope this helps.

    Thanks.

    ZHU

  • Suggested answer
    Dallefeld Profile Picture
    Dallefeld 11,423 User Group Leader on at
    RE: Modify base application codeunit or create extension to add on to it

    Vaishnavi gave you the answer...make the field number is sales header and sales header archive and BC takes care of the rest!

  • Suggested answer
    Vaishnavi J Profile Picture
    Vaishnavi J 3,056 on at
    RE: Modify base application codeunit or create extension to add on to it

    Hi,

    Create a field in Sales Order Archive table and display it on page.

    Please note if the Field No in Sales Order table and Sales Order Archive table is same then you don't need to add the below logic the code unit part.

    Generally Microsoft uses Transferfield function when transferring the data in two different tables if the Field No. is same then transferfield function will set the data in Archive Table. 

    if the Field No. are different in both the tables then you have to write the below logic .

    codeunit 50044 Events

    {
        Permissions = tabledata Rec_SalesHeaderArchieve = RIMD;

        [EventSubscriber(ObjectType::Codeunit, Codeunit::ArchiveManagement, 'OnAfterAutoArchiveSalesDocument', '', false, false)]
        local procedure OnAfterAutoArchiveSalesDocument(var SalesHeader: Record "Sales Header")
        var
            Rec_SalesHeaderArchieve: Record "Sales Header Archive";
        begin
           Clear(Rec_SalesHeaderArchieve);
            Rec_SalesHeaderArchieve.Reset();
            Rec_SalesHeaderArchieve.SetRange("Document Type", Rec_SalesHeaderArchieve."Document Type"::Order);
            Rec_SalesHeaderArchieve.SetRange("Document Date", SalesHeader."Document Date");
            Rec_SalesHeaderArchieve.SetRange("No.", SalesHeader."No.");
            Rec_SalesHeaderArchieve.SetRange("Version No.", SalesHeader."No. of Archived Versions");
            if Rec_SalesHeaderArchieve.FindFirst() then begin
                Rec_SalesHeaderArchieve.Document Revision Number := SalesHeader.Document Revision Number;
                Rec_SalesHeaderArchieve.Modify(true);
            end;
        end;
    }

    If my answer was helpful to you, please verify it so that other users know it worked. Thank you very much

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,564 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,651 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans