Create task scheduler programmatically

Hello, I would like to know if there is possibility of creating a task scheduler programmatically and how to do it, without having to go through the administration menu.
Thanks and regards.

2 Likes

Hello,
Cuba has no API to create scheduled tasks from code, but there is an ability to add them right to the database by query.
Scheduled tasks are presented in SYS_SCHEDULED_TASK table.

There is an appropriate way to simplify the script development:

  1. Open Administration - Scheduled Tasks menu
  2. Create the required scheduled task.
  3. In the Scheduled Tasks table, find the task and click on it by the right button
  4. Select the ‘System Information’ item
  5. There on the bottom of ‘System Information’ dialog find ‘Script for insert’ button
  6. Click on it and the corresponding SQL becomes available.
  7. Now it is possible to copy it.

So that SQL may be programmatically added to database scripts (UPDATE SCRIPTS or INIT DATA: 30.create-db.sql)
It may be also run at any time as a native query (see https://doc.cuba-platform.com/manual-6.1/nativeQuery.html)

If you really want to create tasks at runtime, you can also create and save ScheduledTask entities. It will be the same as creating tasks in UI.

Thanks Rostislav

Thanks Konstantin.

true

Please tell me how to create a CUBA task Scheduled