Although not a specific "feature" of MoneyWorks, webhooks (i.e. initiating a scripted action in MoneyWorks via a URL) can be easily implemented using a special custom report. This relies on three other MoneyWorks features:
- Reports can be invoked using the MoneyWorks REST APIs;
- Reports can have embedded scripts.
- You can pass parameters to reports in a REST request.
To embed a script in a report, just add a custom control to the report, set it to be a script, and enter your script (it is better to test the script separately in the script editor then paste the working script in).
In the example below we have a script "myHooks" with two handlers, "add" and "cubicvolume".
In the report itself, we can call a script handler by, for example, =myhooks:add(3,4)
.
The report, which we have called "~hook", could be as follows:
There is a variable script
to specify the handler, and variables a
and b
for handler add
, and variables l
, w
and h
for handler cubicvolume
. These variables will be instantiated automatically by the parameters that are passed in the /doreport
REST call (if we also want to run the report from the user interface, the variables would need to be set up as separate custom controls).
To invoke this through a URL (having uploaded the report to the Datacentre)
http://.../REST/.../doreport/report=~hook&script=cubicvolume&l=10&w=5&h=25
which returns 1250.
In practice your scripts will be more capable, for example extracting all new customers today as JSON, or a list of stock movements through a particular location.
Notes:
- The report name starts with a tilde (~) so it will not appear in the MoneyWorks Reports menu (and hence can't be invoked through the user interface).
- The handlers in script that are to be called from the report must be
public
. - The scripts use a subset of the mwScripting. In particular the following are not available:
- Alert, Ask, Say or anything that might create or interact with a window or selection
- Any file operations, such as syslog and file_open
- CURL calls
- You will need to have the proper authorisation headers for your REST requests.
Keep in mind also that the report writer itself can do a lot of what you can do in a script in terms of number crunching and data manipulation (the scripts were in fact redundant in our trivial example). Also, to update records in MoneyWorks there is /import
and /evaluate/expr=replacefield(...)
, and of course /export
for extracting selected data.