Skip to content

Lesson 2. Expanding the OnLoad ruleset

We've covered the setting of field states in the OnLoad ruleset practice of the previous lesson. Let's now look at setting of default values on the document.

It is possible (even preferable maybe) to set default values in the template configuration rather than in an OnLoad ruleset, but for our purposes, we'll do this in the ruleset.

Step 1. Add Default values to a new Rental Car

  • Open the ruleset you created in the First Ruleset lesson.
  • Add a new empty rule "ruleSetDefaultValues" (add a comma after the previous last rule)
    ruleSetDefaultValues : {
        ruleCondition : function(ntf) {

        },

        ruleAction : function(ntf) { 

        }
    }

This time, we don't want to run the rule every time we load the document, only when it's a new Rental Car document. We need to define the conditions under which to run this rule. For this purpose, we take advantage of the built in flag of ntf, called ntf.isNew.

  • Add this code to the ruleCondition function:
return (ntf.isNew);
  • Now to add the default values. Add this code to the ruleAction function.
    ntf.document.openedDate = new Date();
    ntf.document.createdByUser = ntf.user.email;

ntf.document is an object that represents the document in memory of the open document in the browser. By changing its properties as above, which match in name to the fields in the template, you modify the value of fields on the browser.

ntf.user represents information from the current user's account.

The entire ruleset should look like this:

{
#include "JayRule Ruleset Overlay JS",

    ruleset : {
        name : 'Acme Rental Car - OnLoad',

        ruleInitialise : {
            ruleCondition : function(ntf) {
                return (ntf.runCount === 1);
            },

            ruleAction : function(ntf) {
            }
        },

        ruleMandateMainFields : {
            ruleCondition : function(ntf) { 
                return true;
            },

            ruleAction : function(ntf) { 
                ft3.mandateField('carMake', true);
                ft3.mandateField('carModel', true);

                ft3.enableField('createdByUser', false);

                ft3.showField('reasonDecommissioned', false);
            }
        },

        ruleSetDefaultValues : {
            ruleCondition : function(ntf) {
                return (ntf.isNew);
            },

            ruleAction : function(ntf) { 
                ntf.document.openedDate = new Date();
                ntf.document.createdByUser = ntf.user.email;
            }
        }
    }
}
  • Save your ruleset.

Step 2. Testing

  • Open a new "Rental Car" document, or reload if already open.

  • The field "Opened Date" should contain the date/time of opening the document. Make a note of this date and time.

  • The field "Created By" should contain your email address.

  • Wait at least a minute

  • Add details to the form, and save the document.

  • Reload the document.

  • The field "Opened Date" should contain that original date/time, not change to a new date/time.

If the above is correct, you have successfully completed expanding the OnLoad ruleset !

Lesson Items covered

  • Writing a new rule
  • Setting conditions on a rule
  • Setting of field values using ntf.document