Search…
Script Types

Triggers

1
// Example of a custom time-based access control mechanism in an account trigger update.
2
var hour = new Date().getUTCHours();
3
if (!(hour >= 8 && hour <= 17)) {
4
throw new Error('Updates are only allowed between 8am and 5pm GMT');
5
}
6
7
// Update the c_random property each time the instance is updated (not directly writable by the account holder).
8
script.arguments.new.update('c_random', Math.random(), {grant: 8});
Copied!
Triggers can be setup to run before and/or after creation, update, and deletion of object instances, as well as before and after session authentications (to allow for custom authentication mechanisms).

Before Trigger

All before triggers are run inline, that is they return false or throw an error from the script to cancel the operation. In object instance before triggers, changes can be made inline to instance properties directly from the script, and made as either the script's calling principal or given a grant. These changes are recorded in the audit logs as separate events.
All before update trigger code should be idempotent - they should return the same result after every operation - as they may run more than once during update conflict resolution events.

After Trigger

An after script runs after the operation that triggered it has completed and is ideally suited for sending notifications. These triggers are guaranteed to run only once.

Routes

A route script creates new API endpoints for an organization. Route matching is available using wildcards, named-parameters and regular expression matching. A route can be set to run as the calling principal or as any other available org principal. ACL can also be applied at the configuration level in order to restrict endpoint access to a set of roles or specific accounts.
1
// example of utilizing a path parameter to output an xml-document.
2
// This route was configured as GET /routes/multiply/:a/:b
3
4
var req = require('request'),
5
res = require('response'),
6
xml = require('xml');
7
8
var response = {
9
total: req.params.a * req.params.b
10
};
11
12
res.setHeader('Content-Type', 'text/xml');
13
res.write( xml.toXml(response) );
Copied!
PUT and POST routes can be configured to parse application/x-www-form-urlencoded form data, as well as accept text/ content-types. When accepting text/ content, the request body will be in raw form. Otherwise, application/json and application/x-www-form-urlencoded are parsed into json objects.

Jobs

1
import notifications from 'notifications';
2
import moment from 'moment';
3
4
// load administrators.
5
const adminIds = org.objects.accounts
6
.find({roles: '000000000000000000000004'})
7
.paths('_id')
8
.skipAcl(true)
9
.grant(4)
10
.map(doc => doc._id);
11
12
// set desired time periods.
13
const startDate = moment(new Date()).utc().subtract(1,'day').startOf('day').toDate(),
14
endDate = moment(new Date()).utc().subtract(1,'day').endOf('day').toDate();
15
16
// aggregate counts.
17
const cursor = org.objects.c_widgets.aggregate()
18
.match({created: {$gte: startDate, $lte: endDate}})
19
.group({_id: null, count: {$count: '_id'}});
20
21
const count = cursor.hasNext() ? cursor.next().count : 0;
22
23
// create report.
24
const report = {
25
c_num_new_widgets: count
26
};
27
28
// send notification to admins.
29
adminIds.forEach(_id =>
30
notifications.send('c_daily_report', report, {recipient: _id})
31
);
Copied!
Jobs are scheduled using standard cron syntax and are suited for maintenance tasks, statistics gathering and data post-processing.

Libraries

A library is a CommonJS library that can be loaded by triggers, jobs, routes, and other library scripts. The format follows that of a common js module. A library is configured with an Export property, the name that will be used to import into other scripts. The name follows the same c_ naming convention as the rest of the api.
For example, a library script with the Export name of c_utils could look like the following:
IMAGE HERE
With the following script:
1
/**
2
* Utility Library
3
*/
4
5
module.exports = {
6
7
foo: function() {
8
return 'bar';
9
},
10
11
baz: function() {
12
return 'qux';
13
}
14
15
}
Copied!
And could then be imported into other scripts like the following
1
/**
2
* My Script
3
*/
4
5
import utils from 'c_utils'
6
7
return utils.foo() // returns 'bar'
Copied!
Last modified 1mo ago