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...
Suggested Answer

Creating a 100 General journal lines

(0) ShareShare
ReportReport
Posted on by 2

Hi ALL,

              I am creating a 100 general journal lines through code, When I call  the report it is not working can anyone help me, I am sharing my code pls check and help me.

report 50141 "Gen Journal Creation"
{
    UsageCategory = ReportsAndAnalysis;
    ApplicationArea = All;
    ProcessingOnly = true;

    dataset
    {
        dataitem("Gen. Journal Line"; "Gen. Journal Line")
        {
            column(Posting_Date; "Posting Date")
            {

            }
            column(Account_No_; "Account No.")
            {

            }
            trigger OnAfterGetRecord()
            var
                GenJournalLine: Record "Gen. Journal Line";
                i: Integer;
            begin
                for i := 1 to 100 do begin
                    GenJournalLine.Init();
                    GenJournalLine.Validate("Journal Template Name", 'General');
                    GenJournalLine.Validate("Journal Batch Name", 'Default');
                    GenJournalLine."Line No." += 10000;
                    GenJournalLine.Insert(true);
                    GenJournalLine."Posting Date" := WorkDate();
                    GenJournalLine."Document Type" := GenJournalLine."Document Type"::Payment;
                    GenJournalLine."Account Type" := GenJournalLine."Account Type"::Customer;
                    GenJournalLine."Account No." := '20000';
                    GenJournalLine.Amount := 100.00;
                    GenJournalLine."Bal. Account Type" := GenJournalLine."Bal. Account Type"::"G/L Account";
                    GenJournalLine."Bal. Account No." := '10100';
                    GenJournalLine.Modify();
                end;
                Message('100 journals are created.');
            end;
        }
    }

    requestpage
    {
        layout
        {
            area(Content)
            {
                group(GroupName)
                {
                    // field(Name; SourceExpression)
                    // {
                    //     ApplicationArea = All;

                    // }
                }
            }
        }

        actions
        {
            area(processing)
            {
                action(ActionName)
                {
                    ApplicationArea = All;

                }
            }
        }
    }

    var
        myInt: Integer;
}
Thanks & Regards,
Sams.
I have the same question (0)
  • Suggested answer
    Amit Baru Profile Picture
    3,037 on at

    Hi,

    Updated code:

    report 50141 "Gen Journal Creation"
    {
        UsageCategory = ReportsAndAnalysis;
        ApplicationArea = All;
        ProcessingOnly = true;

        dataset
        {
            dataitem(Integer; Integer)
            {
                DataItemTableView = sorting(Number);
                trigger OnPreDataItem()
                var
                begin
                    Setrange(Number, 1);
                end;

                trigger OnAfterGetRecord()
                var
                    GenJournalLine: Record "Gen. Journal Line";
                    i: Integer;
                begin
                    for i := 1 to 100 do begin
                        GenJournalLine.Init();
                        GenJournalLine.Validate("Journal Template Name", 'General');
                        GenJournalLine.Validate("Journal Batch Name", 'Default');
                        GenJournalLine."Line No." += 10000;
                        GenJournalLine.Insert(true);
                        GenJournalLine."Posting Date" := WorkDate();
                        GenJournalLine."Document Type" := GenJournalLine."Document Type"::Payment;
                        GenJournalLine."Account Type" := GenJournalLine."Account Type"::Customer;
                        GenJournalLine."Account No." := '20000';
                        GenJournalLine.Amount := 100.00;
                        GenJournalLine."Bal. Account Type" := GenJournalLine."Bal. Account Type"::"G/L Account";
                        GenJournalLine."Bal. Account No." := '10100';
                        GenJournalLine.Modify();
                    end;
                    Message('100 journals are created.');
                end;
            }
        }

        requestpage
        {
            layout
            {
                area(Content)
                {
                    group(GroupName)
                    {
                        // field(Name; SourceExpression)
                        // {
                        //     ApplicationArea = All;

                        // }
                    }
                }
            }

            actions
            {
                area(processing)
                {
                    action(ActionName)
                    {
                        ApplicationArea = All;

                    }
                }
            }
        }

        var
            myInt: Integer;
    }
    Regards
    Amit Sharma
  • Suggested answer
    Nitin Verma Profile Picture
    21,698 Moderator on at

    Hi,

    More fine way to do that.

    report 50141 "Gen Journal Creation"
    {
        UsageCategory = ReportsAndAnalysis;
        ApplicationArea = All;
        ProcessingOnly = true;

        trigger OnPostReport()
        var
            GenJournalLine: Record "Gen. Journal Line";
            i: Integer;
            LineNo: Integer;
        begin
            LineNo := 10000;
            for i := 1 to 100 do begin
                GenJournalLine.Init();
                GenJournalLine.Validate("Journal Template Name", 'General');
                GenJournalLine.Validate("Journal Batch Name", 'Default');
                GenJournalLine."Line No." := LineNo;
                GenJournalLine.Insert(true);
                GenJournalLine."Posting Date" := WorkDate();
                GenJournalLine."Document Type" := GenJournalLine."Document Type"::Payment;
                GenJournalLine."Account Type" := GenJournalLine."Account Type"::Customer;
                GenJournalLine."Account No." := '20000';
                GenJournalLine.Amount := 100.00;
                GenJournalLine."Bal. Account Type" := GenJournalLine."Bal. Account Type"::"G/L Account";
                GenJournalLine."Bal. Account No." := '10100';
                GenJournalLine.Modify();
                LineNo += 10000;
            end;
        end;
    }
  • Satish Profile Picture
    2 on at

    Hi Nitin,

                I have tried your code it worked well for first time but when I call the report for second time it is showing the error,

     pastedimage1664451672865v1.png

    Thanks & Regards.

    Sams

  • Suggested answer
    Chris Dsilva Profile Picture
    40 on at

    Hi Sams

    The error is because you haven't posted the journal lines imported.

    You might need to post them before the next 100 lines else you could create a new variable for Gen. Jnl Line and do a findlast and receive the line no.

    Hope this helps!

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    Your problem is in this codeline.

    GenJournalLine."Line No." := LineNo;

    You need to increment the line no for every line you insert.

    Define an integer variable LineNo

    Increment the LinoNo for every line you insert

    LineNo +=10000;

    GenjorunalLine."Line No." := LineNo;

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    You are getting the error because the line no is part of the primary key on the journal line. So you need to make sure the line no. is unique for every line.

    In the base application the line no. is increased by 10000 for every line that is inserted so there is space to put lines in between two existing lines.

  • Obaidullah Profile Picture
    35 on at

    1.Need to increment Line No every time you want to create lines

    Second solution

    2. Before creating General journal lines get last line No and increment every time in Loop

  • Suggested answer
    Amit Baru Profile Picture
    3,037 on at

    Hi,

    I think in my given code, this was already done.

    Updated code:

    report 50141 "Gen Journal Creation"

    {

       UsageCategory = ReportsAndAnalysis;

       ApplicationArea = All;

       ProcessingOnly = true;

       dataset

       {

           dataitem(Integer; Integer)

           {

               DataItemTableView = sorting(Number);

               trigger OnPreDataItem()

               var

               begin

                   Setrange(Number, 1);

               end;

               trigger OnAfterGetRecord()

               var

                   GenJournalLine: Record "Gen. Journal Line";

                   i: Integer;

               begin

                   for i := 1 to 100 do begin

                       GenJournalLine.Init();

                       GenJournalLine.Validate("Journal Template Name", 'General');

                       GenJournalLine.Validate("Journal Batch Name", 'Default');

                       GenJournalLine."Line No." += 10000;

                       GenJournalLine.Insert(true);

                       GenJournalLine."Posting Date" := WorkDate();

                       GenJournalLine."Document Type" := GenJournalLine."Document Type"::Payment;

                       GenJournalLine."Account Type" := GenJournalLine."Account Type"::Customer;

                       GenJournalLine."Account No." := '20000';

                       GenJournalLine.Amount := 100.00;

                       GenJournalLine."Bal. Account Type" := GenJournalLine."Bal. Account Type"::"G/L Account";

                       GenJournalLine."Bal. Account No." := '10100';

                       GenJournalLine.Modify();

                   end;

                   Message('100 journals are created.');

               end;

           }

       }

       requestpage

       {

           layout

           {

               area(Content)

               {

                   group(GroupName)

                   {

                       // field(Name; SourceExpression)

                       // {

                       //     ApplicationArea = All;

                       // }

                   }

               }

           }

           actions

           {

               area(processing)

               {

                   action(ActionName)

                   {

                       ApplicationArea = All;

                   }

               }

           }

       }

       var

           myInt: Integer;

    }

    Regards

    Amit Sharma

    www.erpconsultors.com

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    This code should work. 

    I think the other code samples will run into error because the line number is not incremented.

    report 50141 "Gen Journal Creation"
    
    {
    
       UsageCategory = ReportsAndAnalysis;
    
       ApplicationArea = All;
    
       ProcessingOnly = true;
    
       dataset
    
       {
    
           dataitem(Integer; Integer)
    
           {
    
               DataItemTableView = sorting(Number);
    
               trigger OnPreDataItem()
    
               var
    
               begin
    
                   Setrange(Number, 1);
    
               end;
    
               trigger OnAfterGetRecord()
    
               var
    
                   GenJournalLine: Record "Gen. Journal Line";
    
                   i: Integer;
                   LineNo: Integer;
    
               begin
    
                   for i := 1 to 100 do begin
    
                       GenJournalLine.Init();
    
                       GenJournalLine.Validate("Journal Template Name", 'General');
    
                       GenJournalLine.Validate("Journal Batch Name", 'Default');
    
                        LineNo  =10000;
                       GenJournalLine."Line No." := LineNo;
    
                       GenJournalLine.Insert(true);
    
                       GenJournalLine."Posting Date" := WorkDate();
    
                       GenJournalLine."Document Type" := GenJournalLine."Document Type"::Payment;
    
                       GenJournalLine."Account Type" := GenJournalLine."Account Type"::Customer;
    
                       GenJournalLine."Account No." := '20000';
    
                       GenJournalLine.Amount := 100.00;
    
                       GenJournalLine."Bal. Account Type" := GenJournalLine."Bal. Account Type"::"G/L Account";
    
                       GenJournalLine."Bal. Account No." := '10100';
    
                       GenJournalLine.Modify();
    
                   end;
    
                   Message('100 journals are created.');
    
               end;
    
           }
    
       }
    
       requestpage
    
       {
    
           layout
    
           {
    
               area(Content)
    
               {
    
                   group(GroupName)
    
                   {
    
                       // field(Name; SourceExpression)
    
                       // {
    
                       //     ApplicationArea = All;
    
                       // }
    
                   }
    
               }
    
           }
    
           actions
    
           {
    
               area(processing)
    
               {
    
                   action(ActionName)
    
                   {
    
                       ApplicationArea = All;
    
                   }
    
               }
    
           }
    
       }
    
       var
    
           myInt: Integer;
    
    }

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 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans