Skip to content

Basic Ruleset Techniques

Updated pdexter 2023-12-21

Getting values from the form/document

In order to retrieve values as displayed and/or entered on the current form, we use the object ntf.document, followed by the name of the field.

The name of the field is the "name" property of the configured field/component within the corresponding template of the document being looked at.

ntf.document is basically a snapshot of the document as if it was saved at that moment.

Syntax

value = ntf.document.field-name;

Example

// Assuming fields 'firstName' and 'surname' exist on the template/document
var fullName = ntf.document.firstName + ' ' + ntf.document.surname;

Setting values on the form/document

In the same way as for getting values above, setting values is done simply by assigning to ntf.document.field-name.

Syntax

ntf.document.field_name = value;

Example

// Assuming fields 'firstName' and 'surname' exist on the template/document
ntf.document.firstName = 'Algernon';
ntf.document.surname = 'Blackwood';

Using Rule-scope variables

Often you may want to define a complex object for use by both the ruleCondition and the ruleAction functions.

You can declare such an object once under the rule object, instead of having to define it within both the ruleCondition and the ruleAction, and access it via the this keyword.

Eg:

ruleStatusSetDate : {
    onAppTags : ['widget','gizmo'], // <===== shared object variable

    offsetPerStatus : {             // <===== shared object variable
        'New' : 4,
        'Started' : 8,
        'Completed' : 12
    },

    ruleCondition : function(ntf) { 
        return (
            this.onAppTags.some(tag => ntf.document.appTags.includes(tag))
            && ntf.context.fieldChanged === 'status'    
            && !!this.offsetPerStatus[ntf.document.status]
        );
    },

    ruleAction : function(ntf) { 
        ntf.document.operationalStatusChangedDate = new Date();

        var offsetHrs = this.offsetPerStatus[ntf.document.status];
        ntf.document.opStatusETA = ft3.moment().add(offsetHrs, 'hour').toDate();
    }
}

Warning 1:

If your ruleAction is declared with an arrow function, eg ruleAction : ntf => {..., this will not be correctly defined ever within the function.

Warning 2:

If your ruleAction performs a complex function call that requires a callback function, this will no longer refer to the rule bloc. To make it work, assign this to a variable that will persist into the callback function.

Expanding the example above, we would do the following:

    ruleAction : function(ntf) { 
        var ft3 = ntf.scope;
        // Assign ruleBloc variable
        var ruleBloc = this;                

        ntf.document.operationalStatusChangedDate = new Date();

        ft3.fetchStateFlag(ntf.document.status, function(okFlag) {
            if (okFlag) {

                // Utilise ruleBloc variable
                var offsetHrs = ruleBloc.offsetPerStatus[ntf.document.status];

                ntf.document.opStatusETA = ft3.moment().add(offsetHrs, 'hour').toDate();
            }           
        })
    }