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

How can I run a code unit directly or through a batch job in Business Central?

(0) ShareShare
ReportReport
Posted on by 2,209 Moderator

I have a code unit that I want to be able to run either directly or through a batch job in Business Central. I want to have the flexibility to choose how to run the code unit based on my preference without having to modify the code unit each time.

The reason for this is that I don't want the screen to block every time I run that code unit. I want to be able to choose to run it in the background or in a queue so that it doesn't interfere with my work.

What is the best approach to accomplish this? Should I add my code unit to a job on the Job Queue Entries page, If yes, then how can I manage it to run whenever I want?

I would appreciate any guidance or help.

Thank you.

  • Govinda Kumar Profile Picture
    Govinda Kumar 2,209 Moderator on at
    RE: How can I run a code unit directly or through a batch job in Business Central?

    I have created the job queue for my codeunit manually through code and have been able to run it manually at once using RunJobQueueEntryOnce. However, I am still facing an issue where it pops a dialog box to run in the foreground, which blocks my screen when running. What I really want is to run it in the background without any dialog box or schedule it to run later so that it goes into a queue and doesn't run immediately.

    Does anyone have any suggestions or like what I'm doing wrong here?

  • Govinda Kumar Profile Picture
    Govinda Kumar 2,209 Moderator on at
    RE: How can I run a code unit directly or through a batch job in Business Central?

    Thank you NAV_with_NarangYUN ZHUNitin VermaManish Sharma for your response, considering your suggestions of the first option of creating a button on a page and adding the option to run the codeunit in a job or directly seems like the better option for my use case.

    As for running the job, I can either create a new job queue entry and runs it.. when the user selects the option to run the codeunit in a job, or I can manually create the job queue entry beforehand and run that job queue entry at once...what should be the best option... and how can I run the queue without scheduling it.. to run.. 

    Regards

  • ManishS Profile Picture
    ManishS 78 on at
    RE: How can I run a code unit directly or through a batch job in Business Central?

    You can check task scheduler to create you job queue and execute it in background.

    But you would be needing a action button somewhere to start this activity.

    Shopify has this kind of scheduling for products, you can check that as an example.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,309 Moderator on at
    RE: How can I run a code unit directly or through a batch job in Business Central?

    Hi peopleeater ,

    There are two ways and 1st you can create a button on the required page and write your logic how you want to execute that codeunit and 2nd option you can create a report without any dataitem and write your logic on PostReport to run the Codeunit based on the boolean field, and that boolean field you can put on report's request page, so that when you run that report, you can put that boolean value true based on your requirement and run the report and that logic of PostReport will trigger.

    Thanks.

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 77,981 Super User 2025 Season 1 on at
    RE: How can I run a code unit directly or through a batch job in Business Central?

    Hi, maybe my understanding is wrong, but Codeunit can be set in Job Queue, you can put your code in trigger OnRun(), so that it can run directly.

    pastedimage1678331100647v1.png

    Hope this helps.

    Thanks.

    ZHU

  • Suggested answer
    NAV_with_Narang Profile Picture
    NAV_with_Narang 2,238 Moderator on at
    RE: How can I run a code unit directly or through a batch job in Business Central?

    You can create a button on a page, which will create a Job Queue Entry in the background and start running immediately without interrupting your front UI. You can also write the code in such a manner that it deletes this JQ once it has completed it's process. There are examples of creation of Job Queues when you use BC <> CRM OOB integration

    You can also use Page background tasks as illustrated here by Microsoft learn.microsoft.com/.../devenv-page-background-tasks

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans