Skip to content

DirectScript Usage Guide

The DirectScript page can be used as a sandbox to test out ruleset script. It is designed to test both clientside and serverside script.

This removes the need to create a template to work off, then a ruleset script attached to that template.

It can also be useful for demonstrating errors for submission to technical developers to debug an issue.

DirectScript Template ID

c22325e0-6283-11e6-b92d-278e9674be8b

Sample DirectScript page

Screenshot1

Running a client script

A client script is one that normally runs in response to OnLoad, OnFieldChange, or PreSave events. It acts in the browser javascript space, rather than the server nodejs space.

Script created here is executed as part of the document's OnFieldChange process (for the run button click).

To create a client script:

  • Create a function within the first parenthesis block ({}) called runMe
  {
      runMe : function(ntf) {
        var main = this;
        var ft3 = ntf.scope;

        // insert script here
          ...
      }
  }
  • Add the script you want to run.

  • Click the button Run

Output/Debugging

Open a browser debugger console from your browser, to view output.

All calls to ntf.logger will appear in the console, so you can check the running of your script.

You can also present notications via ft3.showNotification and ft3.ModalService.showModal, or even the native alert function.

Running a server side script

A server side script is one that occurs on the server, and is usually in response to a PreSaveServer, PostSave, a System Action or System Schedule.

Script created here is executed as part of the document's PreSaveServer process.

It will be necessary to save the DirectScript document, so you will need to set a Name, and optionally a Description.

To create a server side script:

  • Create a function within the first parenthesis block ({}) called preSaveServer
  {
      preSaveServer : function(ntf, callback) {
        var main = this;
        var ft3 = ntf.scope;

        // insert script here
          ...

      }
  }
  • Add the script you want to run.

  • Set the document's Name field (optionally, Group and Description).

  • Save the document

Setting the Callback

The server script requires a callback to be made at the end of its operation.

The callback function takes two arguments: errorMessage, and successMessage.

If the script is deemed to fail, you can simply call
callback('Something went wrong.').

If the script is deemed to complete successfully, call
callback(null, 'The operation was a success');

You must call callback in all paths of your script, otherwise the document may not save at all, and no output will be available.

Output/Debugging

The resulting logging will appear in the lower pane captioned Output Log. The process handling your script actually writes this logging to a document field called outputLog.

All calls to ntf.logger will appear in this pane, so you can check the running of your script.

Because this runs on the server, you cannot raise notifications as for the client scripting.

Listing

A listing of DirectScript documents should be available through the following listing document:

https://your-formbird-app/form/aca562c0-15a0-11e7-bcda-7bdc57bf9867

Samples

Name : openDocument Test

Group : test

{
    runMe : function(ntf) {
        var main = this;
        var ft3 = ntf.scope;

        var tid = '07e7fbd0-cee3-11e5-aa08-fdabe14f1759';

        var newInfo = {
            commonName : 'Earwig',
            genus : 'Arghenis',
            species : 'Tonsuris'
        };

        ft3.openNewDocument(tid, newInfo, true, function(err) {
            ntf.logger.error(err);
            ntf.logger.info('OpenNewDocument done');
        });

    }
}

Name : UpdateDocument test

Group : test

{
    runMe : function(ntf) {
        var main = this;
        var ft3 = ntf.scope;

        // Enter code here

    },

    preSaveServer : function(ntf, callback) {
        var main = this;
        var ft3 = ntf.scope;
        ntf.userId = ntf.user.documentId; 

        var targetDocId = 'dad19f90-14d5-11e7-bcda-7bdc57bf9867';

        var change = {
            'description' : 'ererw-' + ft3.moment().toISOString()
        };

        ft3.updateDocument(targetDocId, change, ntf.userId, function(err, doc) {
            if (err) {
                var msg = err.message.substr(0,160) + '...';
                callback('Error on update of asset = ' + msg);
            }
            else if (doc) {
                callback(null, 'Asset document updated with stuff');
            }
            else {
                callback('No error no doc');
            }
        } );

    }
}