Remove Inactive users from knowledge base table

Amrutha K V
Tera Contributor

Hi Team,

I have a requirement for removing the inactive managers[kb_managers] from knowledge base[kb_knowledge_base] table using scheduled job. 

 

1. If multiple users[inactive and active users] are present in the Managers[kb_managers] field of Knowledge base [kb_knowledge_base] table, then we have to remove the inactive managers from kb_managers field from knowledge base table and keep the active managers as it is[not need to remove the active managers].


2. If inactive user is an Owner of Knowledge Base, then needs to send an email notification to KB managers that owner is terminated.

This is my Requirement. Could you please suggest and help me with the script for scheduled job if possible.

Thank you!!

8 REPLIES 8

Bert_c1
Kilo Patron

Hi,

 

I use the following script:

 

 

// Remove in-acvite managers from knowledge base
var kbase = new GlideRecord('kb_knowledge_base');
kbase.addQuery('kb_managers','!=', '');
kbase.query();
gs.info("inactiveManagers: Found " + kbase.getRowCount() + " records to process.");
while (kbase.next()) {
	gs.info("inactiveManagers: Title: " + kbase.title + " have managers: " + kbase.kb_managers);
	var managers = kbase.kb_managers.split(',');
	if (managers.length > 0) {
		var activeMgrs = [];
		var activeMgrsNames = [];
		var inactiveMgrsNames = [];
		var updateMgrs = false;
		for (i = 0; i < managers.length; i++) {
//			gs.info("inactiveManagers: Checking Manager: " + managers[i]);
			var usr = new GlideRecord('sys_user');
			usr.addQuery('sys_id', managers[i]);
			usr.addQuery('active', true);
			usr.query();
			if (usr.next()) {
//				gs.info("inactiveManagers: Manager: " + managers[i] + ", active = " + usr.active);
				activeMgrs.push(managers[i].toString());
				activeMgrsNames.push(usr.name);
			}
			else {
				// Account for in-active manager in list
				updateMgrs = true;
				var name = usr.get(managers[i]);
				inactiveMgrsNames.push(usr.name);
			}
		}
		// update knowledge base record with active managers
		if (updateMgrs) {
			kbase.kb_managers = activeMgrs.toString();
			gs.info("inactiveManagers: For: " + kbase.title + ", Setting new Managers = " + activeMgrsNames + ".");
			gs.info("inactiveManagers: For: " + kbase.title + ", Inactive managers = " + inactiveMgrsNames + ".");
			kbase.update();
		}
	}
}

 

 

And for me, I don't care when the corresponding user record was last updated as that would be set for any change to the user record. Not just the 'active' field. Since the goal is to remove in-active users from the 'kb_manager' field.  You can use the above (or any similar script logic) in a Scheduled job that runs once a week.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

I see you are referring to scripting, though why is that? Have you considered using Flow Designer for this and creating a Scheduled Flow? Reading what you are after, no scripting is needed.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Because the OP here seems to want a script, and like me possibly, have not gotten proficient in Flow Designer. I'm sure you know, in many scenarios there are multiple methods to achieve the same result.

Sure the are multiple methods, that's why I'm mentioning Flow Designer. Also mentioning Flow Designer, since if someone is asking such a scripting question, then it's more than likely due to limited (or no) scripting knowledge.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn