Cloud Connected

Thoughts and Ideas from the Gitana Development Team

Running Custom Scripts through Rules Test

You may want a complex action to occur in your project that is specific to your business requirements. We provide a number of out of the box actions that you can easily setup in a Content Rule, but sometimes the requirement goes beyond these functionalities. So, Cloud CMS allows you to write Javascript scripts that will run whenever a rule is triggered. In this example, we will be working with the following definition, call it custom:counter:

{
	"title": "counter",
	"type": "object",
	"properties": {
		"title": {
			"type": "string",
			"title": "Title"
		},
		"count": {
			"type": "number",
			"title": "Count"
		},
		"counting": {
			"type": "boolean",
			"title": "Counting"
		}
	},
	"_parent": "n:node"
}

The goal is to make it so every time this document is updated, the counter will be incremented, but only if the "counting" checkbox is checked. So, go to Rules from the left menu and create a new rule. Go to its Policies and add a policy binding. Set this to bind to all content of a single type, select the policy After a Node is Updated (p:afterUpdateNode), and then on the next screen select your content type to bind to. This will make it so the rule will trigger its actions whenever a node of your content type is updated. policy.jpg

Next, go to actions, add one, and select execute script. From within a script, there is a prepopulated model that allows you to access the platform, repository, branch, and node, accessing them and updating using bindings from the Javascript Driver. So, we can write the following script:

if (typeof(node.data.count) === 'undefined')
{
	node.data.count = 0;
}

var counting = node.data.counting;
if (counting)
{
	node.data.count++;
}

This will need to be on one line in the action, and should look like this: script.jpg

Your rule is now all setup! So now everytime you modify a "counter" node, its count will increment by one.