Article Scheduled Trigger

When an automation should executed periodically and not in direct relation to a specific action, it can be triggered based on a schedule. When executed on a schedule, the automation will execute on the job server.

Triggering Automations using a Schedule

To enable an automation to be executed periodically on a schedule,

  1. On the automation's Overview, locate Triggers, and check Scheduled.
  2. Select the schedule type from the drop-down and provide details required by the schedule type.

Using Scheduled Triggers in Automation Implementations

Each enabled instance of a scheduled automation will appear in the jobs listing (Administration > Monitoring > Jobs) with its next execution time. When the scheduled execution time occurs, the automation is executed. When the automation completes, its next scheduled time will be calculated and the job listing will be updated to reflect the automation's next execution time. When executed manually, details about the schedule are provided through the context_v2_automationTrigger API:

  • Entity. For scheduled triggers, the Entity is always "Job"
  • Event. For scheduled triggers, the Event is always "Executed"
  • Arguments. For scheduled triggers, Arguments provides two properties:
    • IsCancellationRequested. Automations run on a schedule are allowed to run for an extended period of time on the job server. If the job server requests that the current task end, the IsCancellationRequested argument will change to true. For long running automations, this property should be checked periodically and the process should be cancelled if possible when it is set to true.
    • Schedule. The current configured schedule for the automation. This is for reference to enable the automation to know roughly how frequently it is executed.

Example of a Scheduled Trigger

Below is a simple example of utilizing an automation with a scheduled trigger. For this scenario, we would like to deactivate users who have not logged in to the site for a certain amount of time.

Creating the Automation

After following the dialog in Administration > Automation > Automation Studio to create a new automation, we set the schedule to run once daily and to use the service account in order to manage user details.

Any instance of this automation will need to specify a time frame to use when determining which users to deactivate, so in the Configuration section of our automation, we add options for length of time and unit of measure (days or years).

<propertyGroup id="options" labelText="Options">
    <property id="age" labelText="Age" descriptionText="Number of units to measure" dataType="Int" defaultValue="1" />
    <property id="ageUnit" labelText="Unit of Measure" descriptionText="Calculate in days or years" dataType="String" defaultValue="y">
        <propertyValue value="y" labelText="Years" />
        <propertyValue value="d" labelText="Days" />
    </property>
</propertyGroup>

In the Implementation section of our automation, we can then retrieve these values, ensure we have them, and ensure they are valid before continuing execution. In this process we also generate the cutoff date we will use for later comparison.

var unit = core_v2_widget.GetStringValue('ageUnit', '');
if(unit === '') {
    return;
}

var age = core_v2_widget.GetIntValue('age', 0);
if(age === 0) {
    return;
}

var currentDate = core_v2_utility.CurrentDate;
var cutoff = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0, 0, 0);

if(unit === 'y')
{
    cutoff.setFullYear(cutoff.getFullYear() - age);
}
else if (unit === 'd')
{
    cutoff.setDate(cutoff.getDate() - age);
}
else return;

All that's left is to iterate through the site's users and check their LastVisitedDate. This can be done through use of the user scripting/widget API. We check each user's LastVisitedDate and update the user to Disapproved when necessary.

var pageIndex = 0;
var done = false;
while(!done) {
    var users = core_v2_user.List({
       SortBy: 'LastVisitedDate',
       SortOrder: 'Ascending',
       PageSize: 100,
       PageIndex: pageIndex
    });
    
    for(var i = 0; i < users.Count; i++) {  
        if(!users[i].IsSystemAccount && users[i].JoinDate !== null && users[i].JoinDate <= cutoff) {
            core_v2_user.Update({ AccountStatus: 'Disapproved', Id: users[i].Id });
        }
    }
    
    if(users.TotalCount <= 100 * (pageIndex + 1)) {
        done = true;
    }
    
    pageIndex++;
}

Configuring an Automation Instance

In Administration > Automation > Automations, we create a new Automation instance and specify the type. This then allows us to specify the configuration details we created the options for in our automation.

Once the instance is saved, the automation will run according to the specified schedule until all instances are disabled or deleted.

Source code

Full source is attached below.

<automation name="Deactivate old users" version="11.0.0.0" description="" id="3b7178ca397e4b4eb8e4bc31665f732a" executeAsServiceUser="false" trigger="Job" schedule="1m" lastModified="2019-06-24 14:43:00Z" provider="31f486cc-03b5-42b6-b5df-81cdd976aa59">
	<executionScript language="JavaScript"><![CDATA[var unit = core_v2_widget.GetStringValue('ageUnit', '');
if(unit === '') {
    return;
}

var age = core_v2_widget.GetIntValue('age', 0);
if(age === 0) {
    return;
}

var currentDate = core_v2_utility.CurrentDate;
var cutoff = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0, 0, 0);

if(unit === 'y')
{
    cutoff.setFullYear(cutoff.getFullYear() - age);
}
else if (unit === 'd')
{
    cutoff.setDate(cutoff.getDate() - age);
}
else return;

var pageIndex = 0;
var done = false;
while(!done) {
    var users = core_v2_user.List({
       SortBy: 'LastVisitedDate',
       SortOrder: 'Ascending',
       PageSize: 100,
       PageIndex: pageIndex
    });
    
    for(var i = 0; i < users.Count; i++) {  
        if(!users[i].IsSystemAccount && users[i].JoinDate !== null && users[i].JoinDate <= cutoff) {
            core_v2_user.Update({ AccountStatus: 'Disapproved', Id: users[i].Id });
        }
    }
    
    if(users.TotalCount <= 100 * (pageIndex + 1)) {
        done = true;
    }
    
    pageIndex++;
}]]></executionScript>
	<configuration><![CDATA[<propertyGroup id="options" labelText="Options">
    <property id="age" labelText="Age" descriptionText="Number of units to measure" dataType="Int" defaultValue="1" />
    <property id="ageUnit" labelText="Unit of Measure" descriptionText="Calculate in days or years" dataType="String" defaultValue="y">
        <propertyValue value="y" labelText="Years" />
        <propertyValue value="d" labelText="Days" />
    </property>
</propertyGroup>]]></configuration>
</automation>