Show/Hide Toolbars

The current user is the user who is triggering a rule or invoking an action. When you want code to execute regardless of the current user's rights, you change the current user to the system user. Because the user running the code can change, you may need to retrieve the current user.

To retrieve the user object for the current user, use the utilityService.getCurrentUser() method.

The following code sample retrieves the username of the current user. This code is useful when the current user is a required condition. You can use the username to determine whether the current user meets the condition.

String username = UtilityService.getCurrentUser().getUsername();

The following code sample retrieves the contact record of the current user.

User currentUser = utilityService.getCurrentUser();

Contact userContact = userService.getUserAccountForUser(user).getContact();

The following code sample retrieves the groups to which the current user belongs.

utilityService.getCurrentUser.getGroupList();

The following code sample checks the list of groups for the current user to find out if the user is part of a specific group. You can use this code as a template to check other conditions in addition to the group membership of the current user.

// Initialize services.

UtilityService utilityService = platform.getUtilityService();

GroupService groupService = platform.getGroupService();

 

//get group list for user.

User currentUser = utilityService.getCurrentUser();

List<Group> groupsForUser = groupService.getGroupsForUser(currentUser);

 

//Check to see if specific group is part of the list.

for (Group group : groupsForUser) {

if (group.getDisplayName().equals("specifiedGroupName")) {

return false;

}

}

return true;