Read and Review Automation Anywhere Documentation

Automation 360

Close Contents

Contents

Open Contents

Create a trigger using Package SDK

  • Updated: 12/11/2020
    • Automation 360 v.x
    • Explore
    • RPA Workspace

Create a trigger using Package SDK

When using Automation 360, you can create a trigger using the SDK package.

Using a trigger

Use Automation 360 to create a trigger. A trigger launches a bot when certain conditions are met and waits until conditions have changed or stopped. When the conditions of the trigger are matched, the run method is called to signal the trigger.

Required annotations

For creating a trigger, the following annotations are required:

Annotation Usage
BotCommand Use the BotCommand annotation with the trigger as commandType. This ensures that the plain old Java object (POJO) is suitable for creating the trigger with Automation 360.
CommandPkg These values are used when creating a package. Provide a name, label, and description to the annotation.
Idx Annotate all the parameters and member variables that are required and help with the validation check. Alternatively, they might be displayed in the interface for the input. Provide the index (Idx) and the type.
Pkg Annotate all the parameters and member variables that will be shown in the interface. This annotation will be ignored if it is not accompanied by the Idx.
StartListen Starts the trigger listener.
TriggerId A trigger ID, which is required for stopping the trigger.
TriggerRunnable Provides the runnable. Calling the runnable sends a signal that launches a bot associated with a trigger.

Use case example

The following use case shows how to create a timer trigger that triggers a bot at a regular interval.

  1. Create the POJO class with the business logic:
    public class TriggerDemo {
    	
    	private static final Timer TIMER = new Timer(true);
    	
    	public void startTrigger(Double interval) {
    		TimerTask timerTask = new TimerTask() {
    
    			@Override
    			public void run() {
    				// Do nothing
    
    			}
    		};
    
    		TIMER.schedule(timerTask, interval.longValue());
    	}
    
    }

    Multiple triggers of the same type can exist in a bot. All triggers are identified by a triggerId.

  2. Add a triggerId to the class:
    @TriggerId
    	private String triggerUid;
    	
    	public String getTriggerUid() {
    		return triggerUid;
    	}
    
    	public void setTriggerUid(String triggerUid) {
    		this.triggerUid = triggerUid;
    	}
  3. Store the TimerTask separately so that when you want to stop a specific trigger, use the triggerId.
    private static final Map<String, TimerTask> taskMap = new ConcurrentHashMap<>();
    	
    	public void startTrigger(Double interval) {
    		TimerTask timerTask = new TimerTask() {
    
    			@Override
    			public void run() {
    				// Do nothing
    
    			}
    		};
            taskMap.put(this.triggerUid, timerTask);
    		TIMER.schedule(timerTask, interval.longValue());
    	}
  4. Start a trigger when conditions are met. Accept a runnable and call it appropriately.
    @TriggerRunnable
    	private Runnable runnable;
    	
    	public Runnable getRunnable() {
    		return runnable;
    	}
    
    	public void setRunnable(Runnable runnable) {
    		this.runnable = runnable;
    	}
    	
    	public void startTrigger(Double interval) {
    		TimerTask timerTask = new TimerTask() {
    
    			@Override
    			public void run() {
    				runnable.run();
    
    			}
    		};
            taskMap.put(this.triggerUid, timerTask);
    		TIMER.schedule(timerTask, interval.longValue());
    	}
  5. Stop the trigger, either individually (use a triggerId), or stop all triggers at the same time (StopAllTriggers).
    /*
    	 * Cancel all the task and clear the map.
    	 */
    	@StopAllTriggers
    	public void stopAllTriggers() {
    		taskMap.forEach((k, v) -> {
    			if (v.cancel()) {
    				taskMap.remove(k);
    			}
    		});
    	}
    
    	/*
    	 * Cancel the task and remove from map
    	 *
    	 * @param triggerUid
    	 */
    	@StopListen
    	public void stopListen(String triggerUid) {
    		if (taskMap.get(triggerUid).cancel()) {
    			taskMap.remove(triggerUid);
    		}
    	}
  6. Add the remaining annotations.
    @BotCommand(commandType = BotCommand.CommandType.Trigger)
    @CommandPkg(label = "Demo Trigger", description = "Demo Trigger", icon = "email.svg", name = "demoTrigger")
    public class TriggerDemo {
    //Other code
    
    	@StartListen
    	public void startTrigger(@Idx(index = "1", type = AttributeType.NUMBER)
    	@Pkg(label = "Please provide the interval to trigger in seconds", default_value = "120", default_value_type = DataType.NUMBER)
    	@GreaterThan("0")
    	@NumberInteger
    	@NotEmpty
    	Double interval) {
    	//Other code
    	}
    
    }
Send Feedback