- Overview
- Custom activities
- Migrating Activities to .NET 6
- Release Notes
- Building Workflow Analyzer Rules
- Building Activities Project Settings
- Creating Custom Wizards
- Prioritize Activities by Scope
- UiPath.Activities.Api.Base
- UiPath.Studio.Activities.Api
- UiPath.Studio.Activities.Api.Activities
- UiPath.Studio.Activities.Api.BusyService
- UiPath.Studio.Activities.Api.ExpressionEditor
- UiPath.Studio.Activities.Api.Expressions
- UiPath.Studio.Activities.Api.Licensing
- UiPath.Studio.Activities.Api.Mocking
- UiPath.Studio.Activities.Api.ObjectLibrary
- UiPath.Studio.Activities.Api.PackageBindings
- UiPath.Studio.Activities.Api.ProjectProperties
- UiPath.Studio.Activities.Api.ScopedActivities
- UiPath.Studio.Activities.Api.Settings
- UiPath.Studio.Activities.Api.Wizards
- UiPath.Studio.Activities.Api.Workflow
- UiPath.Studio.Api.Controls
- UiPath.Studio.Api.Telemetry
- UiPath.Studio.Api.Theme
- Robot JavaScript SDK
- Triggers SDK
- How to Create a Custom Trigger
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.
In order to create a custom trigger, the following are required:
- Microsoft Visual Studio
- Microsoft .NET Framework v4.6.1 or higher
- Open Microsoft Visual Studio and choose to Create a new project. The Project selection window is displayed.
- Select Class Library (.NET Framework), and click Next. The Configure your new project window is displayed.
-
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.
- From the Tools menu, select Options. The Options window is now displayed.
- Expand the NuGet Package Manager entry, and select Package Sources.
-
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. - Click the Tools menu, and from the NuGet Package Manager entry select Manage NuGet Packages for Solution.... The NuGet - Solutions tab is displayed.
- 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.
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;
}
StartMonitor
and StopMonitor
methods. This is done to dictate the behavior of the trigger for monitoring specific events.
<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.