web
You’re offline. This is a read only version of the page.
close
Skip to main content
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,304 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.

I have the same question (0)
  • Suggested answer
    NAV_with_Narang Profile Picture
    2,294 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

  • Suggested answer
    YUN ZHU Profile Picture
    95,307 Super User 2025 Season 2 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
    Nitin Verma Profile Picture
    21,698 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.

  • ManishS Profile Picture
    86 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.

  • Govinda Kumar Profile Picture
    2,304 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

  • Govinda Kumar Profile Picture
    2,304 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?

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,377

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,696 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,512 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans