About Custom Triggers

When you create an automation on your site, you must select a trigger that causes the automation to run. A trigger is an event that occurs on your site and gets reported to Wix.

An automation only runs when the event that defines its trigger occurs. For example, if you select the Member logs in trigger, your automation will run when a member logs in. Most triggers are based on similar business logic, like payment information or contact management.

However, you may want to run your automation when an event occurs that is not defined by existing triggers. Examples include:

  • Creating a new contact when a site visitor clicks a certain button.
  • Send a chat message offering help when a member inputs an invalid value into a text input field.
  • Send a coupon if a visitor is on your site for a certain period of time.

With the Custom Trigger API, you can run an automation for any event that occurs by triggering the automation from your code. This gives you greater control and flexibility over when your automations run.

Supported IDEs

Once you select the code trigger in the automation builder, you can write your code in the following IDEs:

  • The editor (Wix Studio and Wix Editor).
  • The Wix IDE (Wix Studio).
  • Your local IDE (Wix Studio and Wix Editor).

Trigger configuration

There are 2 parts to the trigger configuration:

The trigger ID

When you select the Velo code trigger as your automation trigger, Wix generates a unique trigger ID. When you call Run Trigger, you pass it the trigger ID. The method then runs the automation associated with the ID. The result is that you can run the automation from anywhere in your site code, simply by calling Run Trigger with the relevant ID.

The payload schema

In addition to the ID, you can optionally define a payload schema for the trigger, and pass a payload to Run Trigger. The structure of the payload schema is generated from sample data you enter when you configure the trigger. For example, if you enter sample data like this:

Copy

Your payload schema will have a structure like this:

Copy

Payload fields are optional, so even if you define a payload schema, you’re not required to pass a payload when you call Run Trigger. If you choose to pass a payload, you can omit fields. Any fields you include in the payload should match the key and type defined in the schema.

For example, if your payload structure looks like the one above, you can pass the following payloads to Run Trigger:

Copy

Make sure, however, that any fields required by actions are passed. For example, if subsequent actions need to access contact data, be sure to pass the contact ID field.

Contact data in the payload schema

If your payload schema contains one or more UUID fields, you can mark one of those fields as a contact ID. When the payload is passed to Run Trigger, Wix looks up the ID in the contact ID field. If the ID corresponds to an existing contact, Wix retrieves the contact’s data and attaches that data to the payload. The data can then be passed to any subsequent actions.

Note: If the ID corresponds to a new contact, you can save the contact data by executing the Create a Contact action after the trigger.

Error handling

Here are some common errors you might encounter and their causes:

Error MessageCause
Automation not foundNo automation is found for the trigger ID passed to runTrigger(). This may be because the automation was deleted.
This automation can only be executed within its designated applicationThis only applies to preinstalled automations. An app other than the one that created the automation tries to run it.

See also

Did this help?