Skip to main content

Notifications

Announcements

No record found.

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,203 Super User 2024 Season 1

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,203 Super User 2024 Season 1 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,203 Super User 2024 Season 1 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 6,578 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,091 Super User 2024 Season 1 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 74,115 Super User 2024 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
    NAV_with_Narang Profile Picture
    NAV_with_Narang 2,236 Super User 2024 Season 1 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

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans