Need to remove a user from specific group if users not logged in 30 days But those group need to call from sys_properties

varma2
Mega Sage

 

Hi All,

 

Need to remove a user from specific group if users not logged in 3o days.

Ex : I have group called ( ITSM_Group ) in that group we have multiple users, If any user not logged in more than 30days we need to remove the user from the group.

But we need to create a property for the group and then we need to call that property through SCHEDULED job and then it looks the lost login user on group and it removed the users from group.

 

Please suggest, How can i create property for the groups and how to call the property through SCHEDULED job

Thanks ALL,

7 REPLIES 7

Allen Andreas
Administrator
Administrator

Hi,

You can review the documentation for creating a system property as that gives information that you'd find useful.

So you can create a string system property with the sys_id of the group as the value or use a comma separated list of sys_ids for multiple groups.

Then in your scheduled job, you can use GlideRecord to query the database and review the relevant group's membership records, look at their last login, if great than 30 days, then delete the record, which removes them from the group.

You can access the system property by using (EXAMPLE😞

var propExample = gs.getProperty('property_name');

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

Below is the glide record  which we normally use in  the JOB, But how can we tweak the script which include the sys property.

Please provide the modification of the script that will help me.

 

var arr = [];

var gr = new GlideRecord("sys_user");
var gdt = new GlideDateTime();
gdt.addDays(-30);// User not logged in 30 days
gr.addQuery("last_login_time","<=",gdt);
gr.addActiveQuery();
gr.query();
while(gr.next()){
	arr.push(gr.getUniqueValue());
}

var grp = new GlideRecord("sys_user_grmember");
grp.addQuery("user", "IN", arr.toString());
grp.addQuery("group", "b85d44954a3623120004689b2d5dd60a");
grp.query();
grp.deleteMultiple();

Thanks.

Hello,

If my initial reply above was Helpful, please mark it as such.

As far as your script, if you're only trying to remove users from specific groups who haven't logged in in 30 days, then you don't need to query the entire sys_user table unnecessarily.

You could try something like:

var sysProp = gs.getProperty('name_of_property_here'); //comma separated list of group sys_ids

var grp = new GlideRecord("sys_user_grmember");
grp.addQuery("group", "IN", sysProp.toString().split(','));
grp.query();
while (grp.next()) {

var gr = new GlideRecord("sys_user");
var gdt = new GlideDateTime();
gdt.addDays(-30);// User not logged in 30 days
gr.addQuery('sys_id', grp.getValue('user'));
gr.addQuery("last_login_time","<=",gdt);
gr.addActiveQuery();
gr.query();
if (gr.next()){
	gr.deleteRecord();
}

Please use the idea above and tweak from here.

Please mark reply as Helpful/Correct, if applicable. Thanks!

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

Thanks for your support its work fine but i need to add one more condition which is

 

I need remove user if last_login_time more then 30 and also i need to remove the user if last_login_time field value is Empty.

 

Please help.

 

Thanks,

Hari