Script

How to Format Rules with JavaScript Expressions

JavaScript expressions can be used with the following syntax:

Script(<YOUR_SCRIPT>)

In newer versions of Squidex, the user interface has been improved and custom input fields have been introduced which allows selection of the syntax and adds the necessary prefix automatically.

Basic Syntax

The scripting engine supports almost all ES6 features with a few restrictions.

Therefore, it is best to use the Javascript template string syntax and just reference properties directly:

Script(`${event.appId.id}`)
Script(`${event.appId.Name}`)
Script(`${event.user.id}`)
Script(`${event.user.email}`)

// For content events
Script(`${event.schemaId.id}`)
Script(`${event.schemaId.Name}`)
Script(`${contentUrl()}`)
Script(`${contentAction()}`)
Script(`${event.data.city.de}`)

Special Functions

Squidex provides a set of general helper functions for scripting and rule formatting.

A value list can be found in the documentation concerning scripting helper methods here:

Scripting Helper Methods

Additionally, there are also methods which are only available for rule formatting.

Name
Description

contentAction()

The status of the content, when the event is a content event.

Otherwise null.

contentUrl()

The URL to the content in the Management UI, when the event is a content event. Otherwise null.

assetContentUrl

The URL to download the asset, when the event is an asset event. Otherwise null. This URL does not include the app name and is therefore not recommended.

assetContentAppUrl

The URL to download the asset by ID, when the event is an asset event.

Otherwise null.

assetContentSlugUrl

The URL to download the asset by slug, when the event is an asset event.

Otherwise null.

complete(value)

If you use an asynchronous operation, just like getAssets you have to tell the script engine, which value should be returned. Therefore you have call complete(value) with the result value. If you do not call this method, the result of the last statement is used.

Examples

Resolve References

You can use scripting to resolve references. You must pass over an array of content IDs and a callback (that is invoked) with the resulting list of content items.

Script(
    getReferences(data.references.iv, function (references) {
        var actual1 = `Text: ${references[0].data.field1.iv} ${references[0].data.field2.iv}`;
        var actual2 = `Text: ${references[1].data.field1.iv} ${references[1].data.field2.iv}`;

        complete(`${actual1}\n${actual2}`);
    });
)

Or a single reference:

Script(
    getReference(data.references.iv[0], function (references) {
        var actual1 = `Text: ${references[0].data.field1.iv} ${references[0].data.field2.iv}`;

        complete(`${actual1}`);
    })
)

Resolve References

You can use scripting to resolve assets. You have to pass over an array of asset IDs and a callback (that is invoked) with the resulting list of assets.

Script(
    getAssets(data.assets.iv, function (assets) {
        var actual1 = `Text: ${assets[0].fileName} ${assets[0].id}`;
        var actual2 = `Text: ${assets[1].fileName} ${assets[1].id}`;

        complete(`${actual1}\n${actual2}`);
    });
)

Or a single asset:

Script(
    getAsset(data.assets.iv[0], function (assets) {
        var actual1 = `Text: ${assets[0].fileName} ${assets[0].id}`;
        
        complete(`${actual1}`);
    });
)

Conditional Formatting

You can use if-statements and other JavaScript language features for conditional formatting.

In the following example, different payloads have been created, depending on the asset size:

Script(
    if (event.fileSize > 100000) {
        return `I just uploaded a large image ${event.fileName}`;
    } else {
        return `I just uploaded a small image ${event.fileName}`;
    }
)

Last updated