Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Creating a 100 General journal lines

Posted on by 75

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.
Categories:
  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Creating a 100 General journal lines

    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;
    
    }

  • Suggested answer
    Amit Baru Profile Picture
    Amit Baru 3,025 on at
    RE: Creating a 100 General journal lines

    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

  • Obaidullah Profile Picture
    Obaidullah 35 on at
    RE: Creating a 100 General journal lines

    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
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Creating a 100 General journal lines

    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.

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Creating a 100 General journal lines

    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
    Chris Dsilva Profile Picture
    Chris Dsilva 40 on at
    RE: Creating a 100 General journal lines

    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!

  • Satish Profile Picture
    Satish 75 on at
    RE: Creating a 100 General journal lines

    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
    Nitin Verma Profile Picture
    Nitin Verma 21,025 Moderator on at
    RE: Creating a 100 General journal lines

    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;
    }
  • Suggested answer
    Amit Baru Profile Picture
    Amit Baru 3,025 on at
    RE: Creating a 100 General journal lines

    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

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