Remove the inactive created by user from schedule table.

Abhilasha_123
Tera Contributor

Hi Team,

 

If the 'Created by' user of schedule item record of Schedule [sys_trigger] table is inactive, then we have to pull all the scheduled items which is created by that inactive user and send an email notification to the manager of the created by user that is this scheduled item is still required or not. 
Once the manager replies for the same email that it is not required, then we have to delete that schedule item record from schedule table [sys_trigger].

 

We have to implement this requirement via scheduled job. Could you please help me with the script if possible.

 

Thank you!

1 ACCEPTED SOLUTION

Tushar
Kilo Sage
Kilo Sage

Hi @Abhilasha_123 ,

 

You can use the following script  -

function scheduledJob() {
    // Create an array to store the sys_ids of inactive users
    var inactiveUserIds = [];

    // Step 1: To find inactive users and collect their sys_ids
    var inactiveUsers = new GlideRecord('sys_user');
    inactiveUsers.addQuery('active', 'false'); // Inactive users
    inactiveUsers.query();

    while (inactiveUsers.next()) {
        inactiveUserIds.push(inactiveUsers.sys_id.toString());
    }

    // Step 2: To find and process schedule items created by inactive users
    var scheduleItems = new GlideRecord('sys_trigger');
    scheduleItems.addQuery('created_by', 'IN', inactiveUserIds.join(',')); // Created by inactive users
    scheduleItems.query();

    while (scheduleItems.next()) {
        var inactiveUserId = scheduleItems.created_by.toString();

        // Send an email notification to the manager of the inactive user
        var manager = findManager(inactiveUserId);
        if (manager) {
            var subject = 'Scheduled item from inactive user';
            var body = 'This is to inform you that a scheduled item was created by an inactive user. Please let us know if this scheduled item is still required.';
            sendEmail(manager, subject, body);
        }

        // Optionally, delete the schedule item once it's no longer required
        // scheduleItems.deleteRecord();
    }
}

//  to find the manager of an inactive user
function findManager(inactiveUserId) {
    var user = new GlideRecord('sys_user');
    if (user.get(inactiveUserId)) {
        return user.manager;
    }
    return null; // Manager not found
}

// sending an email
function sendEmail(recipient, subject, body) {
    var email = new GlideEmailOutbound();
    email.setSubject(subject);
    email.setBody(body);
    email.addRecipient(recipient);
    email.send();
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

 

View solution in original post

1 REPLY 1

Tushar
Kilo Sage
Kilo Sage

Hi @Abhilasha_123 ,

 

You can use the following script  -

function scheduledJob() {
    // Create an array to store the sys_ids of inactive users
    var inactiveUserIds = [];

    // Step 1: To find inactive users and collect their sys_ids
    var inactiveUsers = new GlideRecord('sys_user');
    inactiveUsers.addQuery('active', 'false'); // Inactive users
    inactiveUsers.query();

    while (inactiveUsers.next()) {
        inactiveUserIds.push(inactiveUsers.sys_id.toString());
    }

    // Step 2: To find and process schedule items created by inactive users
    var scheduleItems = new GlideRecord('sys_trigger');
    scheduleItems.addQuery('created_by', 'IN', inactiveUserIds.join(',')); // Created by inactive users
    scheduleItems.query();

    while (scheduleItems.next()) {
        var inactiveUserId = scheduleItems.created_by.toString();

        // Send an email notification to the manager of the inactive user
        var manager = findManager(inactiveUserId);
        if (manager) {
            var subject = 'Scheduled item from inactive user';
            var body = 'This is to inform you that a scheduled item was created by an inactive user. Please let us know if this scheduled item is still required.';
            sendEmail(manager, subject, body);
        }

        // Optionally, delete the schedule item once it's no longer required
        // scheduleItems.deleteRecord();
    }
}

//  to find the manager of an inactive user
function findManager(inactiveUserId) {
    var user = new GlideRecord('sys_user');
    if (user.get(inactiveUserId)) {
        return user.manager;
    }
    return null; // Manager not found
}

// sending an email
function sendEmail(recipient, subject, body) {
    var email = new GlideEmailOutbound();
    email.setSubject(subject);
    email.setBody(body);
    email.addRecipient(recipient);
    email.send();
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar