Get All the Users who has not been assigned a task?

Kiddy
Tera Guru

Script/report to get all users who are not assigned to any task

1 ACCEPTED SOLUTION

Snehangshu Sark
Mega Guru

Hi @Kiddy  @Kiddy,

 

I am not clear about a few things -

1. what type of task you are targeting here

2. group of users you want to check the availability 

 

See the below code, logically it's correct and tested, you need to tweak a few queries and it should work for you.

 

/*
 * created two arrrays to store blocked Users (blockedUsr) and avaibale Users (availableUsr)
*/
var blockedUsr = [], availableUsr = [];
// get the blocked User first
var assignedUsr = new GlideAggregate("task");
// change the below query based on your requirement, (here, I considered incident, problem, change_request)
assignedUsr.addEncodedQuery("sys_class_name=incident^ORsys_class_name=problem^ORsys_class_name=change_request^stateIN1,2^assigned_toISNOTEMPTY");
assignedUsr.groupBy('assigned_to');
assignedUsr.query();
gs.info(("count: "+assignedUsr.getRowCount()));
while(assignedUsr.next()){
	// store all unique blocked Users in blockedUsr array
	blockedUsr.push(assignedUsr.assigned_to.toString());
}
gs.info("blockedUsr: "+blockedUsr);

// next part is to get the available Users
var usr = new GlideRecord("sys_user");
// change the below query based on your requirement
usr.addEncodedQuery("department=221f3db5c6112284009f4becd3039cc9");
// exclude all blocked users
usr.addEncodedQuery("sys_idNOT IN"+blockedUsr);
usr.query();
gs.info("usr count: "+usr.getRowCount());
while(usr.next()){
	// store all unique available Users in availableUsr array
	availableUsr.push(usr.sys_id.toString());
}
gs.info("availableUsr: "+availableUsr);

 

Regards,

Snehangshu Sarkar

 

Please mark my answer as correct if it resolves your query.

View solution in original post

1 REPLY 1

Snehangshu Sark
Mega Guru

Hi @Kiddy  @Kiddy,

 

I am not clear about a few things -

1. what type of task you are targeting here

2. group of users you want to check the availability 

 

See the below code, logically it's correct and tested, you need to tweak a few queries and it should work for you.

 

/*
 * created two arrrays to store blocked Users (blockedUsr) and avaibale Users (availableUsr)
*/
var blockedUsr = [], availableUsr = [];
// get the blocked User first
var assignedUsr = new GlideAggregate("task");
// change the below query based on your requirement, (here, I considered incident, problem, change_request)
assignedUsr.addEncodedQuery("sys_class_name=incident^ORsys_class_name=problem^ORsys_class_name=change_request^stateIN1,2^assigned_toISNOTEMPTY");
assignedUsr.groupBy('assigned_to');
assignedUsr.query();
gs.info(("count: "+assignedUsr.getRowCount()));
while(assignedUsr.next()){
	// store all unique blocked Users in blockedUsr array
	blockedUsr.push(assignedUsr.assigned_to.toString());
}
gs.info("blockedUsr: "+blockedUsr);

// next part is to get the available Users
var usr = new GlideRecord("sys_user");
// change the below query based on your requirement
usr.addEncodedQuery("department=221f3db5c6112284009f4becd3039cc9");
// exclude all blocked users
usr.addEncodedQuery("sys_idNOT IN"+blockedUsr);
usr.query();
gs.info("usr count: "+usr.getRowCount());
while(usr.next()){
	// store all unique available Users in availableUsr array
	availableUsr.push(usr.sys_id.toString());
}
gs.info("availableUsr: "+availableUsr);

 

Regards,

Snehangshu Sarkar

 

Please mark my answer as correct if it resolves your query.