How to add phone number field for the new user?

Jamie Sammons, modified 3 Years ago. New Member Posts: 4 Join Date: 9/9/22 Recent Posts

I am using UserLocaleService to add my user i want to save user phonenumber in database how can i add them

My register user function

 

private static User addQEUser(String firstName, String lastName, String screenName,String email,String password,long siteroleid,ResourceRequest request) throws Exception{
        try {
            ServiceContext serviceContext = new ServiceContext();
            serviceContext.setCompanyId(PortalUtil.getCompanyId(request));
            serviceContext.setScopeGroupId(PortalUtil.getCompany(request).getGroupId());
            long creatorUserId = 0;
            boolean autoPassword = false;
            boolean autoScreenName = false;
            long facebookId = 0;
            String openId = StringPool.BLANK;
            Locale locale = Locale.US;
            String middleName = StringPool.BLANK;
            String fullName = StringPool.BLANK;
            int prefixId = 0;
            int suffixId = 0;
            boolean male = true;
            int birthdayMonth = Calendar.JANUARY;
            int birthdayDay = 1;
            int birthdayYear = 1970;
            String jobTitle = StringPool.BLANK;
            long[] groupIds = new long[1];
            groupIds[0] = siteId;
            long[] organizationIds = null;
            long[] roleIds =null;
            long[] userGroupIds = null;
            boolean sendEmail = true;
            long[] siteRole = new long[1];
            siteRole[0] = siteroleid;
            System.out.println("add user beofre");
            User user = UserLocalServiceUtil.addUser(
                    creatorUserId, companyId, autoPassword, password, password,
                    autoScreenName, screenName, email, facebookId, openId,
                    locale, firstName, lastName, "("+screenName+")", prefixId, suffixId, male,
                    birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
                    organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
            System.out.println("add user"+user);
            user.setAgreedToTermsOfUse(true);
            user.setEmailAddressVerified(false);
            user.setPasswordReset(false);
            user.setReminderQueryQuestion("What is your user ID?");
            user.setReminderQueryAnswer(screenName);
            UserGroupRoleLocalServiceUtil.addUserGroupRoles(user.getUserId(), siteId,siteRole);
            user = UserLocalServiceUtil.updateUser(user);
            return user;
        }
        catch (SystemException e) {
            System.out.println("EXCEPTION IN ADD USER 1"+e);
            throw new Exception(e.getLocalizedMessage());
        }
        catch (PortalException e) {
//            e.printStackTrace();
            System.out.println("EXCEPTION IN ADD USER 2"+e.getLocalizedMessage());
            throw new Exception(e.getLocalizedMessage());
            
        }
        catch(Exception e) {
            System.out.println("EXCEPTION IN ADD USER 3"+e);
            throw new Exception(e.getLocalizedMessage());
            
        }
    }
 

Jamie Sammons, modified 1 Year ago. New Member Posts: 24 Join Date: 6/13/23 Recent Posts

We can use custom fields which can be done via control panel, But since you are creating programmatically, Use Liferay's Expando API.

long phoneNumber = ParamUtil.getLong(request, "phoneNumber");
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
        long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());
        ExpandoTable expandoTable = null;
        ExpandoColumn expandoColumn = null;
        try {
            expandoTable = ExpandoTableLocalServiceUtil.getTable(themeDisplay.getCompanyId(), classNameId,
        "UserPhoneNumber");
        expandoColumn=ExpandoColumnLocalServiceUtil.getColumn(themeDisplay.getCompanyId(), classNameId,expandoTable.getName(), "PhoneNumber");
                } catch (NoSuchTableException nste) {
                    nste.getMessage();
        try {

                        ExpandoTableLocalServiceUtil.addTable(themeDisplay.getCompanyId(), classNameId, "UserPhoneNumber");
                        ExpandoColumnLocalServiceUtil.addColumn(expandoTable.getTableId(), "PhoneNumber",
                                ExpandoColumnConstants.STRING);
                    } catch (PortalException e) {
                        e.printStackTrace();
                    }
                }
        ExpandoValueLocalServiceUtil.addValue(classNameId, expandoTable.getTableId(),
                                expandoColumn.getColumnId(), user.getUserId(), phoneNumber);

Use this code as reference and place inside your method. Expando row, value, column, expandotable will be affected in DB.