Schedule Cron Jobs with Azure Functions
Overview
This article introduces how to set up cron jobs in Azure Functions to periodically post data into Kintone.
Attention
- The libraries
request
andrequest-promise
used in this article have been deprecated.
It is recommended rewrite the code usingaxios
orhttp.request
, where HTTP requests can be made.
For more information, refer to the following links: Moment.js
is in maintenance mode and it is recommended to migrate to an alternative library that can handle dates.
One alternative library isLuxon
. There is an introductory article on how to implement it in Kintone customization.
For more information, refer to the following article:
Customize Date Formats
Sample Image
Two Kintone Apps are used in this example: an Expense Report App and an Expense Summary App. Users use the Expense Report App to record their expenses, such as travel expenses. The Expense Report App has a workflow set up, where users will need to get approval from their manager when the expense is submitted.
A scheduled program runs at 2:00AM on the first day of the month. It sums up all of the expenses for each department from the previous month, and adds a record to the Expense Summary App for each department.
Prepare Azure Functions
Follow the
Create your first function in the Azure portal
article on the Microsoft Azure website to set up Azure Functions.
Navigate to the Advanced Tools menu, then to Go->. Select CMD from the Debug Menu in the header to display the
Kudu
console. Install the following packages through the console:
-
request (package to execute HttpRequests)
1
$ npm install request --save-dev
-
request-promise (package to use Promises)
1
$ npm install request-promise --save-dev
-
moment (package to operate on dates)
1
$ npm install moment --save-dev
Refer to Get Multiple Records with Azure Functions on how to install the packages.
Prepare the Kintone App
This article uses the following App template, containing the Expense Report App and the Expense Summary App:
AzureFunctions integration (scheduled event).zip
Refer to the
App Templates
page in the Kintone Site for how to import App templates. Refer to the
Creating an App from a Template
page on how to create Apps from templates.
Sample Code
The code in this integration will process as follows:
- Get data of all approved records in the Expense Report App** from the previous month.
- Sum up travel expenses by department.
- Add a record to the Expense Summary App for each department, containing data of the summed up expenses.
Prepare the following code for Azure Functions. Replace BASE_URL
, APP_ID_AGG
, APP_ID_AGG_TRANS
, API_TOKEN_AGG
and API_TOKEN_TRANS
with their relative information. Refer to the
Generating API Tokens
article on the Kintone Help site for more information on API Tokens.
|
|
Set up the Scheduler on Azure Functions
Set up the scheduler using Cron expressions in the Schedule field.
|
|
Use the below table as reference for scheduled timings.
Schedule | Cron expression {second} {minute} {hour} {day} {month} {day of the week} |
---|---|
Trigger every 5 minutes | 0 */5 * * * * |
Trigger every hour | 0 0 * * * * |
Trigger every two hours | 0 0 */2 * * * |
Trigger every hour from 9 AM to 5 PM | 0 0 9-17 * * * |
Trigger every day at 9:30 AM | 0 30 9 * * * |
Trigger every weekday at 9:30 AM | 0 30 9 * * 1-5 |
Test the Integration
The cron is set up to run every day at 2:00AM. Check the Kintone App after this time to see if new records were automatically posted.
When the code runs, Azure Functions should first retrieve records from the Expense Report App. The request is queried so that records that have the status of Completed, and are from the previous month are retrieved.
Azure Functions then should aggregate the total expenses per Department. A record should then be added in to the Expense Summary App for every Department.