How to get last login of a user in servicenow portal who have ITIL ROLE

avinashdube
Tera Guru

Hi 

I am trying to get the last logins of user into servicenow portal  who have ITIL role 

The last login field on user table i am not sure whether it gives for backend or portal or for both 

can anyone help on this 

I want to remove only those users from ITil ROLE who have not logined in backend 


1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@avinashdube 

last login tracks login info and doesn't tell if user logged in backend or portal

you can run a fix script or background script for your requirement.

Note: run this in DEV and test first

var inactiveDays = 90; // Adjust as needed
var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.addQuery('roles', 'CONTAINS', 'itil'); // Has ITIL role
grUser.addQuery('last_login_time', 'RELATIVELE@dayofweek@ago@' + inactiveDays);
grUser.addQuery('user_name', 'NOT LIKE', 'Mid.Server'); // Exclude service accounts
grUser.query();

var count = 0;
while (grUser.next()) {
    var grRole = new GlideRecord('sys_user_has_role');
    grRole.addQuery('user', grUser.sys_id.toString());
    grRole.addQuery('role.name', 'itil');
    grRole.query();
    while (grRole.next()) {
        grRole.deleteRecord();
        count++;
    }
    gs.info('Removed ITIL role from inactive user: ' + grUser.user_name);
}
gs.info('Total roles removed: ' + count);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron

@avinashdube 

last login tracks login info and doesn't tell if user logged in backend or portal

you can run a fix script or background script for your requirement.

Note: run this in DEV and test first

var inactiveDays = 90; // Adjust as needed
var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.addQuery('roles', 'CONTAINS', 'itil'); // Has ITIL role
grUser.addQuery('last_login_time', 'RELATIVELE@dayofweek@ago@' + inactiveDays);
grUser.addQuery('user_name', 'NOT LIKE', 'Mid.Server'); // Exclude service accounts
grUser.query();

var count = 0;
while (grUser.next()) {
    var grRole = new GlideRecord('sys_user_has_role');
    grRole.addQuery('user', grUser.sys_id.toString());
    grRole.addQuery('role.name', 'itil');
    grRole.query();
    while (grRole.next()) {
        grRole.deleteRecord();
        count++;
    }
    gs.info('Removed ITIL role from inactive user: ' + grUser.user_name);
}
gs.info('Total roles removed: ' + count);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Kilo Sage
  1. Navigate to the Table: Go to sys_user.list in the application navigator.
  2. Apply Filters:
    • Active is true
    • Roles contains itil
  3. Configure Columns: Add the Last login or Last login time field to the list view to see the data. (sort it to latest updated)TanushreeMaiti_0-1770372204820.png

     

 

Alternative (Detailed Log): Query the sys_user_login_history table for more granular, historical data. 

Please mark this response as Helpful & accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: