SDK
latest
false
Banner background image
Developer Guide
Last updated Mar 23, 2024

How to Create a Custom Trigger

Automation projects benefit from triggers, which monitor the machine activity for specific events in order to fire particular actions. Triggers can be configured through the Monitor Events framework, but you can also build custom ones, as explained in this guide.

Prerequisites

In order to create a custom trigger, the following are required:

  • Microsoft Visual Studio
  • Microsoft .NET Framework v4.6.1 or higher

Creating and Configuring the Project

  1. Open Microsoft Visual Studio and choose to Create a new project. The Project selection window is displayed.
  2. Select Class Library (.NET Framework), and click Next. The Configure your new project window is displayed.
  3. Provide a project name, location, solution name, and framework. Make sure you select .NET Framework 4.6.1 framework or higher. When all fields are filled in, click Create. The new project is created and the designer window is displayed.



  4. From the Tools menu, select Options. The Options window is now displayed.
  5. Expand the NuGet Package Manager entry, and select Package Sources.
  6. Add a new package source, and fill the Name field with UiPath Official Feed and the Source field with https://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.json. Click OK to confirm and save changes.


  7. Click the Tools menu, and from the NuGet Package Manager entry select Manage NuGet Packages for Solution.... The NuGet - Solutions tab is displayed.
  8. Search for the UiPath.Platform reference and select it. In the panel on the right, select the project for which to add the reference, and then click Install. Make sure you check the Include prerelease box and you install the UiPath.Platform v20.8 reference or higher.

Writing the Trigger Code

Once the references are added to the project, it's time to write the code, which should look something like this:

using System; 
using System.Activities; 
using System.Threading; 
using UiPath.Platform.Triggers; 
 
public class TimerTrigger : TriggerBase<TimerTriggerArgs> 
{ 
 //it is recommended to use Variable to store fields in order for  
 //activities like Parallel For Each to work correctly 
   private readonly Variable<Timer> _timer = new Variable<Timer>();   
  public InArgument<TimeSpan> Period { get; set; }    
protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
        metadata.AddImplementationVariable(_timer); 
        base.CacheMetadata(metadata); 
    }     
  
//in this method you can subscribe to events. It is called when the trigger starts execution 
protected override void StartMonitor(NativeActivityContext context, Action<TimerTriggerArgs> sendTrigger) 
    { 
        var eventIndex = 0; 
        var period = Period.Get(context); 
        _timer.Set(context, new Timer(OnTick, state: null, dueTime: period, period: period)); 
        return;  
void OnTick(object state) => sendTrigger(new TimerTriggerArgs(eventIndex++));    
}     
 
   //this is used for cleanup. It is called when the trigger is Cancelled or Aborted 
   protected override void StopMonitor(ActivityContext context) => _timer.Get(context).Dispose(); 
} 
 
//Each trigger may declare a type that sub-classes TriggerArgs 
//that corresponds to the “args” item in Trigger Scope activity. If no extra info 
//needs to be passed along, TriggerArgs can be used directly 
public class TimerTriggerArgs : TriggerArgs 
{ 
    public int EventIndex { get; } 
 
    public TimerTriggerArgs(int eventIndex) => EventIndex = eventIndex; 
}using System; 
using System.Activities; 
using System.Threading; 
using UiPath.Platform.Triggers; 
 
public class TimerTrigger : TriggerBase<TimerTriggerArgs> 
{ 
 //it is recommended to use Variable to store fields in order for  
 //activities like Parallel For Each to work correctly 
   private readonly Variable<Timer> _timer = new Variable<Timer>();   
  public InArgument<TimeSpan> Period { get; set; }    
protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
        metadata.AddImplementationVariable(_timer); 
        base.CacheMetadata(metadata); 
    }     
  
//in this method you can subscribe to events. It is called when the trigger starts execution 
protected override void StartMonitor(NativeActivityContext context, Action<TimerTriggerArgs> sendTrigger) 
    { 
        var eventIndex = 0; 
        var period = Period.Get(context); 
        _timer.Set(context, new Timer(OnTick, state: null, dueTime: period, period: period)); 
        return;  
void OnTick(object state) => sendTrigger(new TimerTriggerArgs(eventIndex++));    
}     
 
   //this is used for cleanup. It is called when the trigger is Cancelled or Aborted 
   protected override void StopMonitor(ActivityContext context) => _timer.Get(context).Dispose(); 
} 
 
//Each trigger may declare a type that sub-classes TriggerArgs 
//that corresponds to the “args” item in Trigger Scope activity. If no extra info 
//needs to be passed along, TriggerArgs can be used directly 
public class TimerTriggerArgs : TriggerArgs 
{ 
    public int EventIndex { get; } 
 
    public TimerTriggerArgs(int eventIndex) => EventIndex = eventIndex; 
}
The next step is to implement the StartMonitor and StopMonitor methods. This is done to dictate the behavior of the trigger for monitoring specific events.
It is important to note that if you want to provide arguments to expand the trigger usage in the event handler, you need to add and configure a <Custom>TriggerArgs class in the designer.

The last step is to build the library and create the NuGet package to use in Studio. The new trigger can now be used inside a Trigger Scope activity.

  • Prerequisites
  • Creating and Configuring the Project
  • Writing the Trigger Code

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.