Scheduled Events

You can create one-time or recurring scheduled events to invoke serverless apps. At the specified time, the App Framework is notified which then invokes the relevant serverless method in the app.

Sample Use Cases

  • Reminders: You can be notified about a specific event scheduled at a particular time. For example, you can trigger an SMS five minutes before a meeting.
  • Data Sync: You can poll an external product every hour for updates and make the same changes in Freshservice.
  • Recurring tasks: You can create daily/weekly reports or tickets to track recurring tasks.

Notes:
1. You can have a maximum of 1000 one-time schedules and 1 recurring schedule.
2. The timeout period for app execution is 20 seconds and the default time zone is UTC. The schedule_at time must be at least 5 minutes from the current time.

Payload

At the specified time, the corresponding method in the server.js file is invoked and the following payload is passed to the app.

Copied
1
2
3
4
5
6
7
8
9
10
11
12
{ "account_id" : "value", "event" : "onScheduledEvent", "region" : "value", "timestamp" : "value", "domain" : "value", "data" : {}, "iparams" : { "Param1" : "value", "Param2" : "value" } }
EXPAND ↓

The following table lists the payload attributes.

Attribute Type Description
account_id number The Freshservice account ID.
event string The name of the event (onScheduledEvent).
region string The region where the app is installed, either "US", "IND", "EUC", or "AUS".
timestamp number Represents the time (epoch format) when the event was received.
domain string The Freshservice account domain.
data JSON The payload that was passed while creating the schedule. The payload should be of JSON type and the size should not exceed 4 KB.
iparams object Installation parameters.

Sample payload

Copied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{ "account_id": 12345, "event": "onScheduledEvent", "timestamp" : 1496400354326, "region" : "US", "domain": "xyz.freshservice.com", "data": { "account_name": "Jacob", "account_id": 3, "country": "France" }, "iparams": { } }
EXPAND ↓

Registration

To register a scheduled event and the corresponding callback:

  1. From your app’s root directory, navigate to the manifest.json file.
  2. Include the events attribute, specifying the scheduled event and the corresponding callback methods as follows:
    Copied Copy
    1
    2
    3
    4
    5
    "events": { "onScheduledEvent": { "handler": "onScheduledEventHandler" } }

    Note: Include only one callback method for an event. Multiple events can access the same callback method.

  3. Navigate to the server.js file.
  4. In the exports block, enter the callback function definition as follows:
    Copied Copy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    exports = { onScheduledEventHandler: function(payload) { console.log("Logging arguments from onScheduledEvent: " + JSON.stringify(payload)); if(payload.data.account_id = "3") { //your code to perform any actions } } };

Schedule

You can use the following methods to create, update, and delete schedules.

To create a one-time schedule - server.js Copied
1
2
3
4
5
6
7
8
9
10
$schedule.create({ name: "ticket_reminder", data: {ticket_id: 100001}, schedule_at: "2018-06-10T07:00:00.860Z", }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. });

To create a recurring schedule - server.js Copied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$schedule.create({ name: "ticket_reminder", data: {ticket_id: 10001}, schedule_at: "2018-06-10T07:00:00.860Z", repeat: { time_unit: "minutes", frequency: 5 } }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. });
EXPAND ↓

To fetch a schedule - server.js Copied
1
2
3
4
5
6
7
8
$schedule.fetch({ name: "ticket_reminder" }) .then(function(data) { //"data" is a json with name, data and schedule_at used to create the schedule }, function(err) { // “err” is a json with status and message. });

To update a schedule - server.js Copied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$schedule.update({ name: "ticket_reminder", data: {ticket_id: 10001}, schedule_at: "2018-06-10T07:00:00.860Z", repeat: { time_unit: "hours", frequency: 1 } }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. });
EXPAND ↓

To delete a schedule - server.js Copied
1
2
3
4
5
6
7
8
$schedule.delete({ name: "ticket_reminder" }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. });

The parameters and their description are listed in the table.

PARAMETERTYPEDESCRIPTION
namestringUnique string to identify the schedule.
datajsonData that needs to be passed to the schedule event handler when the event is fired.
schedule_attime (ISO format)Time at which the schedule needs to be triggered.
Note:
For recurring schedules (with time_unit as days), the hour and minutes at which the scheduled event must be fired will be taken from the schedule_at value.
time_unitstringTime unit (minutes, hours, or days) for which the recurring schedule is created.
frequencynumberFrequency of execution of an event with respect to the time unit.

Testing

Notes:
1. Use the latest version of Chrome browser.
2. Ensure that the onScheduledEvent.json file, which contains the sample payload to test scheduled events, is available at <app's root directory>/server/test_data.

When testing scheduled events on your computer, schedules will be triggered at the specified time and frequency after they are created or updated.