Working With Business Rules Scripting

As noted elsewhere, Business Rules Scripting is a feature of Data Abstract that allows you to add EcmaScript (more commonly referred to as JavaScript) to the Relativity server to handle business logic.

This is a powerful feature because it allows you to keep the important logic for your app in a single place, and should you need to tweak it or change it in the future, you can quickly update the scripts without needing to rebuild & redeploy the client or server applications.

If you haven't already read about Business Rules Scripting, it is recommended that you explore it and the thorough Introduction which goes into great depth of how to make use of Business Rules scripts. The Business Rules Scripting API is also available, it provides detailed information on all of the available events, the global object and the other available objects.

This page is limited to describing how you go about adding a script to a Schema, Table or Command.

Adding a Business Rules Script

Regardless of whether the script you want to add is at the schema, table or command level, the logic is handled within the body of an event.

The events available for you to use in your scripts is constrained by the type of feature you are working with. For instance if you are adding a script that carries out some processing when a command is triggered, then you can add an event that is fired before the command is executed (beforeExecuteCommand()) or one that is fired after the command has been executed (afterExecuteCommand()).

To add a new script, first select the appropriate node in the Schema Tree in the left hand pane of the Schema Modeler. Then click on the disclosure triangle and then click on the Scripts node which will open the Business Rules Script editor view.

Note that there is no script node available in Commands until you first add a Command to the schema.




As you can see, by default no scripts are implemented. If you already have a script provided by someone else, or simply copied from somewhere else, you can simply paste it into the script editor.

Normally to add an event, you click on the Add Event button. This exposes a pop-up menu which shows the events that are available for that particular node. Select the appropriate event from that menu and Schema Modeler will automatically insert a complete method body with the appropriate function signature.

You then add the logic you require into the body of the event function. You have full access to the JavaScript language and the standard JavaScript APIs for writing your logic. You can also take advantage of some additional objects that Data Abstract provides, more information about those is available in the Business Rules Scripting API section.

When you've finished writing the required logic, you simply need to save your changes to the Schema, and they will become live immediately.

Server scripts vs Client & Server

You may have noticed in the script editor pane for Table scripts that it offered two seperate editors, one for Server scripts, and the other for Client & Server scripts. This is to give you control of where the script can be run.

Any event code placed in the Server tab is private to the server and will only be executed there. This includes server specific events, but also any events that would be available on the client as well. Any event code placed in the Client & Server tab will run both on the client and the server.

Note: Events for available on a particular side will be ignored, so if you implement a server only event in the Client & Server tab then it will not run on the client side. Likewise if you implement a client only event then it will be ignored on the server.

While you could implement all the events in the Client & Server tab, it is recommended that you don't. Scripts that you wish to keep private for server side operations should go in the Server tab, as the Client & Server script will be sent to any connecting clients.