---
title: Handling marketing automation triggers
related:
  - https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/marketing-automation/setting-triggers-for-automation-processes.md
  - https://docs.kentico.com/13/custom-development/handling-global-events.md
  - https://docs.kentico.com/13/custom-development/handling-global-events/reference-global-system-events.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

> **Info:** **Enterprise license required**
>
> Features described on this page require the **Kentico Xperience Enterprise** license.

You can influence the triggers that start [Marketing automation](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/marketing-automation.md) processes by implementing custom handlers for the **ProcessTrigger** [global event](https://docs.kentico.com/13/custom-development/handling-global-events.md).

For example, you can use the handler to include additional activity data into the trigger macro resolver. This then allows you to create macro conditions that start a marketing automation process, for example, based on the data submitted by a contact in a column of an online form.

## Example

The following example demonstrates how to add online form submit activity data into a marketing automation trigger macro resolver.

You have a [Marketing automation](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/marketing-automation.md) process that subscribes contacts to a newsletter.

![Marketing automation process that subscribers contacts to a newsletter](https://docs.kentico.com/docsassets/13/handling-marketing-automation-triggers/example_process.png "Marketing automation process that subscribers contacts to a newsletter")

A [trigger](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/marketing-automation/setting-triggers-for-automation-processes.md) starts the process whenever a user submits the _ContactUs_ [form](https://docs.kentico.com/13/managing-website-content/forms.md).

![Trigger that starts the marketing automation process when users submit a form](https://docs.kentico.com/docsassets/13/handling-marketing-automation-triggers/process_trigger.png "Trigger that starts the marketing automation process when users submit a form")

Now you can improve the trigger to make it start the process only when users submit the form with the "example.com" domain in the email field.

1. Open your Xperience solution in Visual Studio (using the **WebApp.sln** file).
2. Create a [custom module class](https://docs.kentico.com/13/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md).

   - Add the class into a custom _Class Library_ project within the solution (in your Xperience administration project, not the MVC live site project)..
3. Override the module's **OnInit** method and assign a handler to the **AutomationEvents.ProcessTrigger.Before** event.

   ```csharp

   using CMS;
   using CMS.DataEngine;
   using CMS.Automation;
   using CMS.OnlineForms;
   using CMS.Activities;
   using CMS.MacroEngine;

   // Registers the custom module into the system
   [assembly: RegisterModule(typeof(ProcessTriggerHandlerModule))]

   public class ProcessTriggerHandlerModule : Module
   {
       // Module class constructor, the system registers the module under the name "ProcessTriggerHandlers"
       public ProcessTriggerHandlerModule()
           : base("ProcessTriggerHandlers")
       {
       }

       // Contains initialization code that is executed when the application starts
       protected override void OnInit()
       {
           base.OnInit();

           // Assigns a handler to the AutomationEvents.ProcessTrigger.Before event
           AutomationEvents.ProcessTrigger.Before += ProcessTrigger_Before;
       }
   }

   ```
4. Define the handler method (inside the custom module class):

   ```csharp

   void ProcessTrigger_Before(object sender, AutomationProcessTriggerEventArgs e)
   {
       // Only applies to activity-based triggers
       if (e.TriggerInfo.TriggerTargetObjectType == ActivityTypeInfo.OBJECT_TYPE)
       {
           foreach (TriggerOptions option in e.Options)
           {
               // Gets the related activity from the macro resolver
               MacroResolver resolver = option.Resolver;
               ActivityInfo activity = resolver.GetNamedSourceData("Activity") as ActivityInfo;

               // Checks if the activity represents the "form submit" action
               if (activity != null && activity.ActivityType.Equals("bizformsubmit"))
               {
                   int formId = activity.ActivityItemID;
                   int itemId = activity.ActivityItemDetailID;

                   // Gets the form
                   BizFormInfo form = BizFormInfo.Provider.Get(formId);

                   if (form != null)
                   {
                       // Gets the submitted form data
                       string className = DataClassInfoProvider.GetClassName(form.FormClassID);
                       BizFormItem item = BizFormItemProvider.GetItem(itemId, className);
                       if (item != null)
                       {
                           // Makes FormData available in the trigger condition evaluation
                           resolver.SetNamedSourceData("FormData", item);
                       }
                   }
               }
           }
       }
   }

   ```
5. Save the class and Rebuild your solution.

You can now use _FormData_ in the trigger condition evaluation. Change the trigger's macro condition to:

```csharp

FormData["Email"].EndsWith("@example.com")

```

The trigger now automatically starts instances of the process when a visitors submits the form with the "@example.com" suffix in the value of the Email field.
