How to create a substitution for a user

When an employee is on holiday, on sick leave, on a business trip, or absent for some other reason, he or she is usually temporarily substituted by another employee.

The information about substitutions needs to be added to the system and shown to other employees, and tasks assigned to the absent employee need to be reassigned to the substitute.

You can learn more about how substitutions work and how to create a substitution manually in the Substitute users in the Help Center.

In this article, you will learn to create substitutions automatically using a custom script.

Let’s say we need to create a substitution for an employee who is going on a business trip. The substitution is created in a business process that starts when a new item of the Business trips app its saved.

When you create a substitution, you need to specify the employee who is going to be absent, the substitute, and the dates of the business trip.

When a new business trip is saved, a business process starts. Its diagram needs to include a script that creates a new substitution.

Script

Let’s add the Script activity to the Business trip process and write the code presented below.

It is important to fill out all the attributes of a substitution in the script. These are Type, Absent user, Substitute, Start date, and End date.

After filling out the attributes, we need to save the new substitution.

async function createReplacement(): Promise<void> {

\t/* Getting the item of the Business trips app
\twhose data will be used to create the substitution */
\tconst trip = await Context.data.trip.fetch();

    // Creating a substitution item
    const newReplacement = System.replacements.create();

    /* Specifying the substitution’s type: Inform (`information`),
    Reassign tasks (`reassign`), or Grant all access permissions (`full`) */

    newReplacement.data.type = newReplacement.fields.type.variants.information;

    // Specifying the absent user (in our case, it is the user who is going on the business trip)
    newReplacement.data.absent = trip.data.traveller;

    // Specifying the substitute
    newReplacement.data.replacement = trip.data.replacer;

    // Specifying the start date and end date using the dates of the business trip from the context
    newReplacement.data.begin = trip.data.trip_start_date;
    newReplacement.data.end = trip.data.trip_end_date;

    // Saving the substitution item
    await newReplacement.save()

}

Result

When the script in the business process is executed, a new substitution appears in the system.