acl

Ramesh_143
Giga Guru

When user can't login to servicenow after 30 days need to restrict catalog item to the users ? How can we restrict this?

4 ACCEPTED SOLUTIONS

VishaalRanS
Tera Guru

Hi @Ramesh_143 

 

To restrict access to catalog items in ServiceNow for users who have not logged in for over 30 days, you can utilize User Criteria and Business Rules.

 

For Business Rules:

 

 

if (current.user.last_login < gs.daysAgo(30)) {
    current.setAbortAction(true); // Prevents access
}

 

 

 

For further, you can refer the below link:

How to restrict users from viewing Catalog Items t... - ServiceNow Community

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

Sai Krishna6147
Mega Guru

Hi @Ramesh_143 

Try to use this acl script

var user = gs.getUser().getRecord();
var lastLogin = user.last_login_time;
var currentDate = new GlideDateTime();
var thresholdDate = currentDate.addDaysUTC(-30);  // 30 days threshold

if (lastLogin == null || lastLogin < thresholdDate) {
   // User hasn't logged in within 30 days, deny access
   answer = false;
} else {
   // Allow access
   answer = true;
}

 

 

If the answer is useful, please the accept the solution and click on helpful sign

View solution in original post

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Ramesh_143 ,

 

You can follow below step to achieve your requirement :

 

you can create a scheduled job in ServiceNow that identifies users who have not logged in for the last 30 days and adds them to a user criteria.

Steps to Create the Scheduled Job

  1. Navigate to Scheduled Jobs:

    • Go to System Definition > Scheduled Jobs.
  2. Create a New Scheduled Job:

    • Click New to create a new job.
  3. Configure the Job:

    • Name: Provide a meaningful name (e.g., "Update User Criteria for Inactive Users").
    • Run: Set the frequency (e.g., Daily).
    • Active: Check the box to make it active.
  4. Script for the Scheduled Job:

Here’s a sample script to find users who haven’t logged in for the last 30 days and add them to a user criteria:

 

// Get the user criteria record
var userCriteria = new GlideRecord('sys_user_criteria');
if (userCriteria.get('YOUR_USER_CRITERIA_SYS_ID')) { // Replace with your user criteria sys_id
    
    // Retrieve existing users in the user criteria
    var existingUsers = userCriteria.user.toString().split(','); // Convert to array for checking
    var existingUserSet = new Set(existingUsers); // Use a Set for quick lookup

    // Get current date
    var thirtyDaysAgo = new GlideDateTime();
    thirtyDaysAgo.subtract(30 * 24 * 60 * 60 * 1000); // Subtract 30 days in milliseconds

    // Query users who have not logged in in the last 30 days
    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('last_login', '<=', thirtyDaysAgo);
    userGR.addQuery('active', true);
    userGR.query();

    var newInactiveUsers = [];
    while (userGR.next()) {
        var userId = userGR.sys_id.toString();
        // Check if user is not already in the criteria
        if (!existingUserSet.has(userId)) {
            newInactiveUsers.push(userId); // Collect new user sys_ids
        }
    }

    // Update user criteria with only new inactive users
    if (newInactiveUsers.length > 0) {
        userCriteria.setValue('user',existingUsers.concat(newInactiveUsers).join(',')); // Combine existing and new users
        userCriteria.update(); // Save changes
    }
}

 

Important Points:

  • User Criteria: Replace 'YOUR_USER_CRITERIA_SYS_ID' with the actual sys_id of the user criteria you want to update.
  • Inactive Users: The script collects users who haven’t logged in for more than 30 days and are marked as active.
  • Log Output: You can check the system logs for how many users were added.

Adding to Catalog Item:

Once the user criteria is updated, you can reference this criteria in the Catalog Item under the Not Available related list. This will restrict access to the catalog item for those users who have not logged in in the last 30 days.

 

Please mark my answer as correct and helpful if it meets your requirements.

 

Regards

Moin

 

View solution in original post

brahmandlapally
Mega Guru

Hi @Ramesh_143

Go to service catalog>>user criteria

open new record and fill out some details name,user click on advanced

and paste the below script 

if (current.user.last_login < gs.daysAgo(30)) {
    current.setAbortAction(true); // Prevents access
}

View solution in original post

6 REPLIES 6

brahmandlapally
Mega Guru

Hi @Ramesh_143

Go to service catalog>>user criteria

open new record and fill out some details name,user click on advanced

and paste the below script 

if (current.user.last_login < gs.daysAgo(30)) {
    current.setAbortAction(true); // Prevents access
}

Anantha27
Mega Guru

hi @Ramesh_143 

You can use this code in br advanced 

var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.query();

var thresholdDate = new GlideDateTime();
thresholdDate.addDaysUTC(-30);

while (grUser.next()) {
if (grUser.last_login && grUser.last_login.getGlideObject().getTime() < thresholdDate.getTime()) {
grUser.active = false;
grUser.update();
}
}