TIBCO EBX®
Rules Portfolio Add-on Documentation > Scripting Language
Navigation modeRules Portfolio Add-on Documentation > Scripting Language

Predefined objects

This section describes the predefined objects used in the Scripting Language. The predefined objects are highlighted in the editor as default JavaScript reserved words.

Accessing the current record's property values

In order to access record data, use the 'record' object. The current record is converted to a script bean object and assigned to the 'record' object. The conversion does as follows:

For example, with the given structure of the 'Strategies' table, the script bean of one record on this table is presented as follow:

/Accessing_The_Current_Records_Property_Values.jpg

The following example shows how to access different record properties in the 'Strategies' table.

/10000000000001E8000000B340F4F389.jpg

The Scripting Language supports "navigation" through foreign keys. As shown in the following image, the 'Strategies' table's script bean presents all foreign keys as objects instead of strings. These objects contain all associated records and not just the associated records' primary keys.

/10000000000000BE000000D3FEA4C06A.jpg

The following example shows how to access 'article.libelleCourt' object's data.

/10000000000001AD0000002FEA552425.jpg

In order to access to a record attribute whose name contains a dot such as 'loc.code' field in 'Article' table, you must use the format record.["loc.code"]. The following example shows how to access 'loc.code' field's data.

/10000000000001710000002E9D434CFF.jpg

Even though the foreign key is presented as an object, you only need to assign another associated primary key value to change its association. So, when you 'get' the foreign key value the script returns an object containing all of the associated records. When you 'set' the foreign key value, the parameter is only the primary key of the associated record.

/100000000000010000000021211770A9.jpg

Special Notation

/100002010000000E0000000E86672C34.png

You cannot add a new value to a multi-valued foreign key field. You can only update or delete the old field value.

Updating the current record

The script bean is just a copy of the record. Modifications made in the script bean do not have any affect on the corresponding record until you call the 'save()'method. However, this only works in certain contexts. For example, only an 'Automated rule', or a 'Table set rule' can modify data. Additionally, permissions may prevent a rule from executing. If you are using a different type rule, the 'save()' method returns 'false' and no modifications are committed.

Additionally, you can disable triggers by calling the 'save(isActivated)' method with a 'true' or 'false' parameter. These enable and disable the trigger, respectively. If you call this method without a parameter, triggers default to enabled.

/100000000000028F0000008B37CF8508.jpg

Deleting the current record

You can call the 'del()' method to delete a record. However, just as with the process of updating a record, you can only modify data in certain contexts. You must have sufficient permission and the rule type should be either an 'Automated rule' or a 'Table set rule'. If the correct conditions are not met, the method returns 'false'.

The following example shows how to delete the current employee record:

/10000000000002520000003F4488D06F.png

The 'del()'method returns 'true' if it deletes corresponding record successfully, otherwise it returns 'false'.

Querying data in the current table

The Scripting Language uses the 'query' object to query data in the current table. It also supports the 'records(XPathPredicate)' function to get a list of records - which, are converted to object beans - in the current table based on the specified predicate. To get all records in the current table, set the predicate to 'null' (query.records(null)). If you want to set conditions replace 'null' with an XPath predicate. To get the number of rules use JavaScript's 'length' property.

The following example checks the number of records in the current table. If the number of records is smaller than 5, it returns 'true'. Otherwise, it returns 'false'.

/10000000000002580000007329A04A1D.png

Querying data in the current data set

When defining a rule that involves querying data in a different table from the same data set you most likely "navigate" through the foreign keys. To accommodate this process, the Scripting Language contains the 'getQuery(tablePath)' method.

The script in the following example states, "One article cannot be the substitute article for more than 10 articles."

/10000000000001F00000008BEB0C483D.jpg

The following bullet points breakdown the content of the above script:

Querying data from a data set in the current data space

The 'query' object allows you to query data in a table from a specific data set in the current data space. To accomplish this, the Scripting Language contains the 'getQuery(String datasetName, String tablePath)' method. The query object returned from this method allows you to modify data on a given data set in the current data space.

The following shows an example query:

/1000000000000238000000A97CD34701.jpg

The following bullet points breakdown the content of the above script:

Querying data from a specified data set and data space

You can use the 'query' object to query data in a table from a specific data set and data space. To support this functionality, the Scripting Language contains the 'getQuery(String homeKey, boolean isBranch, String datasetName, String tablePath)' method. However, the query object returned by this method does not allow you to modify data.

The following examples will help you to understand this feature more deeply:

/100000000000029F0000006D764C10D2.jpg

The following bullet points breakdown the content of the above script:

Creating new records in the current table

The 'newRecord()' method returns the object bean for a new record on the current table. You can then assign values to each of the record's fields. As shown below, you must use the 'save()' method to commit the new record to the current table.

/1000000000000258000000539CECB3CF.png

Writing to the log file

As the complexity of your code increases, keeping track of each line becomes more and more important. To assist you in this endeavor the Scripting Language provides the 'log' object to write information to the log file.

The following example shows how to check the current record's name. If the name is not 'null', this returns 'true'. However, anything that returns a value of 'false' gets added to the log file. Four methods - 'error(message)', 'info(message)', 'debug(message)' and 'warn(message)' - write messages to the 'ebx-addon-common.log' file with respective severity levels of 'ERROR', 'INFO', 'DEBUG' and 'WARNING'. You can find that log file in configured logs folder. See section 'Configuring the EBX® logs' in EBX® document for more detail.

/10000000000002580000008C2F7150FE.png

/10000000000002C8000000DEB51D2A83.jpg

Setting a validation message

You can use the 'validation' object to change add-on error messages. This object contains one message that corresponds to the respective severity level.

The following example shows how to check the current record's name. If this returns a value of 'false', an error message displays. In this case, 'The name is null'.

/10000000000002580000007F9E788443.png

The following image shows this error message. The 'validation' object supports other methods, see the appendix for more information.

/Setting_A_Validation_Message.jpg

Getting data set information

You can retrieve information about the current data set using the 'dataset' object. This object specifically applies to permission rules. The following table shows the relationship between the type of information and its related method:

Dataset Information

Script Method

Owner

'getOwner()'

Adaptation name

'getUniqueName()'

Module name

'getModuleName()'

Schema location

'getDataModel()'

The following example shows how to set permissions by checking the owner. If the owner is 'Beveryone' (B is a specific prefix), permission becomes read-only. Otherwise, it becomes read-write.

/100000000000025800000054D8026A6C.jpg

Getting node information

Like 'dataset', the 'node' object only applies to permission rules. This object retrieves information about the current node and is very useful because nodes are frequently used when creating permission rules. Information retrieved can be a path in the schema, category and data type using the 'getPath()', 'getCategory()', 'getDataType()' pre-defined methods. Other methods tied to the 'node' object check whether the current node is a terminal node and whether history is disabled on the current node. These are 'isTerminalValue()' and 'isHistoryDisabled()', respectively. See the appendix for more information.

Example 1: In this example, the permission rule returns the read-only or read-write access permission depending on whether the current node is auto-incremented.

/100000000000025500000057BB320C00.jpg

Example 2: In this example, a line of code is added to show the node's path in the schema to the log file.

/1000000000000257000000771AFE4E1B.jpg

The image below shows the result.

/100000000000025800000107AEDB69D0.jpg

Getting session information

When you create a permission rule, a tool to retrieve current session information is extremely useful. The Scripting Language provides the 'session' object. By using the supported methods, this object can get the current user id, attribute values by name and check whether the current user is in a specific role.

For example, the following piece of scripting code expresses a permission rule. This permission rule controls access permission based on the specific role of the current user. If the role is 'developer', the access permission will be read-only. Otherwise, the access permission will be read-write.

/10000000000001C4000000544606B3B8.jpg

Predefined permission

The add-on provides some predefined keywords that represent access and action permissions. These keywords are used in the permission rule to return the permission as the result of the permission rules.

These keywords are listed in the following table:

Keyword

Access permission

Action permission

HIDDEN

Represents the hidden permission

Represents the hidden permission

READONLY

Represents the read-only permission

N/A

READWRITE

Represents the read-write permission

N/A

ENABLE

N/A

Represents the enable permission

DISABLE

N/A

Represents the disable permission

The following example shows how a permission rule sets read-only permission for the article record if the internal code is > 1000:

/100000000000017C0000006FFD378860.jpg