Scheduled job

HARSHA GOWDA R
Tera Contributor

How to fetch the details from flow designer (particular flow) that has to run after all the stages are completed.The field(date field) will get updated,through scheduled job that has to check the date field and execute the provision and deprovision of roles on that date

3 REPLIES 3

Deepak Shaerma
Kilo Sage

Hi @HARSHA GOWDA R 

could you please explain your requirements a little more for better understanding??

Hi @Deepak Shaerma 

There is a catalog item called "Title change",under this item there is a variable called effective date,so if effective date is empty then after the approvals the new roles are activated and existing roles are deactivated through flow i have done.If the effective date is not empty (some future date is filled in that field) through flow there is field in our custom table ,in that field the effective will get updated and through scheduled job it has to check daily and if the date matches then the new roles should be activated and exisiting roles should be deactivated

Deepak Shaerma
Kilo Sage

meanwhile create a schedule job that runs daily, 

(function() {
    // Query the table with the date field to check against today’s date.
    var gr = new GlideRecord('your_table');
    gr.addQuery('date_field', '=', gs.nowDateTime());
    gr.query();

    while (gr.next()) {
        // Add business logic to provision roles
        provisionRole(gr);

        // Add business logic to de-provision roles
        deprovisionRole(gr);
        
        // Update a field if needed to mark the action as completed
        gr.setValue('status_field', 'processed');
        gr.update();
    }

    // Function to provision role
    function provisionRole(record) {
        // Example code to provision roles
        var userRole = new GlideRecord('sys_user_has_role');
        userRole.initialize();
        userRole.user = record.user;  // Assuming ‘user’ field contains the user for which roles are to be provisioned
        userRole.role = 'desired_role';  // Replace ‘desired_role’ with the actual role ID
        userRole.insert();
    }

    // Function to deprovision role
    function deprovisionRole(record) {
        // Example code to deprovision roles
        var userRole = new GlideRecord('sys_user_has_role');
        userRole.addQuery('user', record.user);  // Assuming ‘user’ field contains the user for which roles are to be de-provisioned
        userRole.addQuery('role', 'desired_role');  // Replace ‘desired_role’ with the actual role ID
        userRole.query();
        if (userRole.next()) {
            userRole.deleteRecord();
        }
    }
})();

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma