Show/Hide Toolbars

Use UserService when working with user records.

Whenever a Service operation requires UserService, set up UserService using the following code:

UserService userService = platform.getUserService();

Creating a User

The following sample creates a user record. You can use the newUser() and newUserAcccount() methods to create a user account and its associated user, the saveUserAccount() method to save the user, and the getUserForUserAccount() method to extract the user from its account.

During user creation, you can use API interface methods to set objects in the user record, including the corresponding contact, username, and password. Before creating a user, a corresponding contact record must already exist in TeamConnect or you must create a new contact using ContactService. As shown in the sample, when creating a user record, you must also create the user account.

//read or create a contact

Contact contact = contactService.readPerson(1L);

 

//create a limited privilege user account.

UserAccount limitedUserAccount = userService.newLimitedPrivilegeUserAccount("username", "password", contact, true);

limitedUserAccount.setActive(true);

limitedUserAccount.setChangePasswordNextLogin(true);

limitedUserAccount.setShortDescription("Description for limited privilage user account");

//get user from limited privilege user account.

User limitedUser = userService.getUserForUserAccount(limitedUserAccount);

 

//create a normal user account.

UserAccount normalUserAccount = userService.newNormalUserAccount("username", "password", contact);

normalUserAccount.setActive(true);

normalUserAccount.setChangePasswordNextLogin(true);

normalUserAccount.setShortDescription("Description for normal user account");

//get user from normal user account.

User normalUser = userService.getUserForUserAccount(normalUserAccount);

 

//create a super user account.

UserAccount SuperPowerfulUserAccount = userService.newSuperUserAccount("username", "password", contact);

SuperPowerfulUserAccount.setActive(true);

SuperPowerfulUserAccount.setChangePasswordNextLogin(true);

SuperPowerfulUserAccount.setShortDescription("Description for a super powerful user account");

//get user from super user account.

User superUser = userService.getUserForUserAccount(SuperPowerfulUserAccount);

Updating a User

The following sample updates a user record. When you update a user, you can use API interface methods to update fields in the user record, including the username, password, and active setting.

//read or create a user account.

UserAccount userAccount = userService.read(1L);

userAccount.setUsername("newUsername");

userAccount.setPassword("newPassword");

userAccount.setActive(true);

Reading a User

The following sample reads a user record. When you read a user, you retrieve the user so that you can make changes to the user account, such as updating or deleting it.

//read a user account.

UserAccount userAccount = userService.read(1L);

//get the user from the user account.

User user = userService.getUserForUserAccount(userAccount);

Finding a User

The following sample find a particular user record. You can use a username to find the user.

User user = userService.findUserByUsername("username");

Deleting a User

The following sample deletes a user record.

UserAccount userAccount = userService.read(1L);

userService.delete(userAccount);

//same as userService.delete(userAccount.getPrimaryKey());