Dynamics 365 Business Central: How to set General Journal Batch via AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to set General Journal Batch via AL. I was asked this question last week.
Journal batches are created based on the journal templates. All journal batches that are created from a specific template have the same structure, default settings, and information defined in the template. However, because these settings are defaults, you can change them for a specific journal batch. For example, from General Journal Templates page: (For each general journal template, a default general journal batch is automatically created, which contains default information from the journal template)

From General Journals:

Typically, you would use batches to separate one user’s entries from another. You can define your own journal batch for the general journal that has your personal layout and settings, such as a dedicated number series.

If we want users to open a specific batch by default when they open General Journals, is it possible? We have discussed Dynamics 365 Business Central: Can we access global variables from page extensions or table extensions? Yes, But…… before, and I updated the following information.
Update 2023.10.12: CurrentJnlBatchName has been added as Protected var by Microsoft

Yes, we can do it with Protected var, but in this post I want to introduce another simpler way. There is actually a standard method here.
When you select Batches on the General Journal Templates page, the General Journal Batches page opens.

On this page, you can select a batch and then choose Edit Journal to open the General Journals page for that batch.

We can use this standard behavior. Let’s look at the standard code.
page 251 “General Journal Batches” -> GenJnlManagement.TemplateSelectionFromBatch(Rec)

Just pass in the Batch you want to open.

Let’s look at a simple example, using this method, we can do the following.

General Journal Batches:

Add a new action on the Users page, Open the user”s General Journals.

Select a user and click the button to open the batch for this user.

Test video:

Source code:

pageextension 50203 UsersExt extends Users
{
    actions
    {
        addfirst(processing)
        {
            action(OpenUsersGeneralJournals)
            {
                ApplicationArea = All;
                Caption = 'Open the user''s General Journals';
                Promoted = true;
                PromotedCategory = Process;
                Image = Open;

                trigger OnAction()
                var
                    GenJnlManagement: Codeunit GenJnlManagement;
                    GenJournalBatch: Record "Gen. Journal Batch";
                    User: Record User;
                begin
                    User.Reset();
                    CurrPage.SetSelectionFilter(User);
                    if User.FindFirst() then
                        if GenJournalBatch.Get(Enum::"Gen. Journal Template Type"::General, User."User Name") then
                            GenJnlManagement.TemplateSelectionFromBatch(GenJournalBatch);
                end;
            }
        }
    }
}

Be careful not to use this method in the OnOpenPage trigger of the General Journal page, because it contains code to open the General Journal page, which will cause an infinite loop.

If you want to change the default batch when opening the General Journal page, you can use the following event.

Test video:

Source code:

codeunit 50203 GenBatchHandler
{
    [EventSubscriber(ObjectType::Page, Page::"General Journal", OnBeforeOpenJournalFromBatch, '', false, false)]
    local procedure "General Journal_OnBeforeOpenJournalFromBatch"(var GenJournalLine: Record "Gen. Journal Line")
    var
        [SecurityFiltering(SecurityFilter::Filtered)]
        GenJnlTemplate: Record "Gen. Journal Template";
        GenJournalBatch: Record "Gen. Journal Batch";
    begin
        if GenJournalBatch.Get(Enum::"Gen. Journal Template Type"::General, UserId) then begin
            GenJournalLine."Journal Template Name" := GenJournalBatch."Journal Template Name";
            GenJournalLine."Journal Batch Name" := GenJournalBatch.Name;
        end;
    end;
}

Very simple, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL