Hi Himanshu,
List<User> usersToUpdate = new List<User>();
for(User u : [Select id, IsActive from User where isActive=true and lastLoginDate <= :DATE.TODAY()-120]){
u.IsActive = false;
usersToUpdate.add(u);
}
if (usersToUpdate.size()>0){
update usersToUpdate;
}
There are some limitation to do this:
1.Cannot deactivate self user record.
2.Cannot deactivate a user record which is referenced in another user record in a hierarchy custom field.
3.Make a Schedule Class apex and invoke via schedule UI in Salesforce. Every night for example.