- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2024 11:02 PM
When user can't login to servicenow after 30 days need to restrict catalog item to the users ? How can we restrict this?
Solved! Go to Solution.
- 969 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2024 11:10 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2024 01:15 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2024 02:17 AM
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
Navigate to Scheduled Jobs:
- Go to System Definition > Scheduled Jobs.
Create a New Scheduled Job:
- Click New to create a new job.
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-16-2024 12:58 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2024 11:10 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2024 01:15 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2024 01:52 AM
It won’t be beneficial once the user logs in, as the lastLogin field retains the current date and time.
Example: If a user logged in last on September 1st and then logs in again on October 10th, the lastLogin field will now reflect October 10th instead of September 1st. In this case, the user will always have access.
I hope you get my point.
Regards
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2024 02:17 AM
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
Navigate to Scheduled Jobs:
- Go to System Definition > Scheduled Jobs.
Create a New Scheduled Job:
- Click New to create a new job.
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.
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