Reputation: 2311
I am creating the user manually by creating the user details form and calling the UserLocalServiceUtil.addUser()
. Everything is working fine I am able to add the new user. On click of the save button in my user details form I am sending the form details to my action class addUser()
. In this method I am calling the following method to create the user.
try {
UserLocalServiceUtil.addUser(creatorUserId, companyId, autoPassword, password1,
password2, autoScreenName, screenName, emailAddress, mySite, openId,
locale, firstName, middleName, lastName, prefixId, suffixId, male,
birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
} catch(Exception e){
System.out.println(e.getMessage());
}
User is creating with above code. But the issue is I want to add the custom field value also while adding the user. the code to add custom field is as follows,
if(user.getExpandoBridge().hasAttribute("manager"))
{
user.getExpandoBridge().setAttribute("manager", manager);
System.out.println("Custom field is avaliable now !!! Update User entry ");
}
else
{
user.getExpandoBridge().addAttribute("manager");
user.getExpandoBridge().setAttribute("manager", manager);
System.out.println("Custom field is not avaliable !!! Update User entry ");
}
Now how can I execute the code in addUser()
method. My problem is I am adding the user programatically by calling the UserLocalServiceUtil.addUser()
so here I don't have newly created user ID. so with out user ID how can I get the user object for new user and exceute the insert logic of custom field.
Any suggestions please..
I need to create the user with custom field value at same time.
Thanks in advance..
Upvotes: 0
Views: 158
Reputation: 632
Try this
try {
User newUser = UserLocalServiceUtil.addUser(creatorUserId, companyId, autoPassword, password1,
password2, autoScreenName, screenName, emailAddress, mySite, openId,
locale, firstName, middleName, lastName, prefixId, suffixId, male,
birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
} catch(Exception e){
System.out.println(e.getMessage());
}
After that, for expando values
if(newUser.getExpandoBridge().hasAttribute("manager"))
{
newUser.getExpandoBridge().setAttribute("manager", manager);
System.out.println("Custom field is avaliable now !!! Update User entry ");
}
else
{
newUser.getExpandoBridge().addAttribute("manager");
newUser.getExpandoBridge().setAttribute("manager", manager);
System.out.println("Custom field is not avaliable !!! Update User entry ");
}
Upvotes: 2
Reputation: 2862
UserLocalServiceUtil.addUser method returns the created user - ie. instance of User class. You can use the instance to add the expando attributes (and, should you need it, it has the id initialized).
Another solution is to create a service wrapper for UserLocalService. A service wrapper extends the original service implementaiton and makes further customizations. In your case, you can override addUser method, let the original implementation add the user (super.addUser(...)) and then add the expando attributes.
For more details, see Overriding a Portal Service in Liferay 6.2 Developer's Guide.
Upvotes: 1