Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can use this code block and put into the 你可以使用下面这段代码将其放入  匹配Bean Shell in  匹配参与者与用户.

In this piece of code, we will get all the users from the group with ID "G-001". Then, we will traverse through each user and obtain each user's assignment count.

...

中。

在这段代码中,我们将获得ID为“G-001”的所有用户。然后,我们将遍历每个用户,并获得每个用户的分配计数。

具有最少分配计数的第一个用户将被选为当前分配的受托人。您可以将其用作未来编码需求的参考。

Code Block
languagejava
import java.util.Collection;
import org.joget.apps.app.service.AppUtil;
import org.joget.directory.model.User;
import org.joget.directory.model.service.ExtDirectoryManager;
import org.joget.workflow.shark.model.dao.WorkflowAssignmentDao;
 
ExtDirectoryManager directoryManager = (ExtDirectoryManager) AppUtil.getApplicationContext().getBean("directoryManager");
WorkflowAssignmentDao workflowAssignmentDao = (WorkflowAssignmentDao) AppUtil.getApplicationContext().getBean("workflowAssignmentDao");
//set groupId
String groupId = "G-001";
//Get users of the group using directory manager, sorted by firstName
Collection userList = directoryManager.getUsers(null, null, null, null, groupId, null, null, "firstName", false, null, null);
//initialize
String assignTo = "";
int lowestAssignmentCount = -1;
Collection assignees = new ArrayList();
//loop through the users
for(Object u : userList){
	User user = (User) u;
	
	//get open assignment count of the current user
	//refer to https://github.com/jogetworkflow/jw-community/blob/5.0-SNAPSHOT/wflow-wfengine/src/main/java/org/joget/workflow/shark/model/dao/WorkflowAssignmentDao.java#L198 to refine search conditions
	int userAssignmentSize = workflowAssignmentDao.getAssignmentSize(null, null, null, null, u.getUsername(), "open");
	
	//System.out.println(u.getUsername() + " has " + userAssignmentSize);
	if(lowestAssignmentCount == -1){
		//assign to the first person first
		lowestAssignmentCount = userAssignmentSize;
		assignTo = u.getUsername();
	}else if(userAssignmentSize < lowestAssignmentCount){
		assignTo = u.getUsername();
		lowestAssignmentCount = userAssignmentSize;
	}
}
//System.out.println("Will assign to " + assignTo);
assignees.add(assignTo);
return assignees;