Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

Generating dates

Posted on by 45

Hello,

am working on a report to generate dates of the month...eg from 1st to 31st for jan,,,I have been given an idea of using dataItem but not yet got it

Any code I can read through?

  • Verified answer
    RE: Generating dates

    Hello,

    You can use the virtual table "Date". Below is a sample that iterates through january in a codeunit, but you can also specify "Date" as a dataitem in a report. Remember to filter on "Period Type". Here, if you want individual days, then filter it on Date:

    MyDate.setrange("Period Type", MyDate."Period Type"::Date);

    If you wanted the months of the year, you would filter on Month.

    In the sample below, run Business Central with MyServer:8080/.../ then click action "Create Dates", (or run the codeunit somehow else).

    Hope this gets you some ideas.

    Sample code:

    codeunit 50100 GenerateDates

    {

       trigger OnRun()

       begin

           CreateDates(20220101D, 20220131D);

       end;

       local procedure CreateDates(FromDate: Date; ToDate: date)

       var

       begin

           MyDate.setrange("Period Type", MyDate."Period Type"::Date);

           MyDate.setrange("Period Start", FromDate, ToDate);

           if MyDate.FindSet() then

               repeat

                   if not Confirm(format(MyDate."Period Start")) then error('Cancelled.');

               until MyDate.Next() = 0;

       end;

       var

           MyDate: Record Date;

    }

    page 50101 RunMyCodeunit

    {

       PageType = Card;

       ApplicationArea = All;

       UsageCategory = Administration;

       actions

       {

           area(Processing)

           {

               action(CreateDates)

               {

                   Caption = 'Create Dates';

                   Promoted = true;

                   PromotedCategory = Process;

                   PromotedIsBig = true;

                   ApplicationArea = All;

                   trigger OnAction()

                   begin

                       codeunit.run(Codeunit::GenerateDates);

                   end;

               }

           }

       }

       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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans