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 Freshsales.
  • Recurring tasks: You can create daily/weekly 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 Copy
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 Identifier of the Freshsales account, auto-generated when the account is configured for a business.
event string Identifier of the event - onScheduledEvent.
region string The region where the app is deployed.
Possible values: US, EU, EUC, AUS, and IND
timestamp number Timestamp of when the scheduled event occurs, specified in the epoch format.
domain string Domain name for the Freshsales account. For example, acn.myfreshworks.com.
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 specified as a JSON object of <parameter name>: <parameter value> pairs.

Sample payload

Copied Copy
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.myfreshworks.com", "data": { "account_name": "John", "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 } } };

Schedules

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

To create a one-time schedule - server.js Copied Copy
1
2
3
4
5
6
7
8
9
10
$schedule.create({ name: "task_reminder", data: {task_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 Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$schedule.create({ name: "task_reminder", data: {task_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 Copy
1
2
3
4
5
6
7
8
$schedule.fetch({ name: "task_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 Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$schedule.update({ name: "task_reminder", data: {task_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 Copy
1
2
3
4
5
6
7
8
$schedule.delete({ name: "task_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.

PARAMETER TYPE DESCRIPTION
name string Unique string to identify the schedule.
data json Data that needs to be passed to the schedule event handler when the event is fired.
schedule_at time (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_unit string Time unit (minutes, hours, or days) for which the recurring schedule is created.
frequency number Frequency of execution of an event with respect to the time unit.

Testing

When a scheduled event is triggered, schedules are run at the specified time and frequency.

For information on how to test external events, see Test the App.