Use GroupService when working with group records.
Whenever a Service operation requires GroupService, set up GroupService using the following code:
GroupService groupService = platform.getGroupService(); |
The following sample creates a group. To create a group, you need to create a group account.
During group creation, you can use API interface methods to set objects in the group, such as its description.
//create a new group account with specified unique name and display name. GroupAccount groupAccount = groupService.newGroupAccount("GroupUniqueName", "GroupDisplayName"); groupAccount.setDescription("Description"); |
The following sample updates a group. When you update a group, you can use API interface methods to update the group account, such as its description.
//get group with specified name. Group group = groupService.getGroupForName("GroupName");
//retrieve the group account from the group. GroupAccount groupAccount = groupService.getGroupAccountForGroup(group); groupAccount.setDescription("newDescription"); |
The following sample reads a group. When you read a group, you retrieve the group account so that you can make changes to it, such as updating or deleting it.
//read group and user accounts. GroupAccount groupAccount = groupService.read(1L); UserAccount userAccount = userService.read(1L);
//get the group and user from their corresponding accounts. Group group = groupService.getGroupForGroupAccount(groupAccount); User user = userService.getUserForUserAccount(userAccount);
groupService.addUserToGroup(group, user); |
The following sample finds a particular group. You can use the unique to find the group.
//get group based on its unique key. Group group = groupService.getGroupForKey("uniqueKey"); //get all groups. List<Group> allGroups = groupService.getAllGroups(); //get all users for a group. List<User> usersForGroup = groupService.getAllUsersForGroup(group); //get all user accounts for group account. GroupAccount groupAccount = groupService.read(1L); List<UserAccount> userAccountsForGroupAccount = groupService.getAllUserAccountsForGroupAccount(groupAccount); //get all groups for a user. User user = userService.findUserByUsername("username"); List<Group> groupsForUser = groupService.getGroupsForUser(user); |
The following sample deletes a group by deleting the group account.
GroupAccount groupAccount = groupService.read(1L); groupService.delete(groupAccount); //same as groupService.delete(groupAccount.getPrimaryKey());; |