Search code examples
javajspliferayliferay-6jsp-tags

how to use EmailAddressLocalService in liferay


I'm try to add an additional email address for some users with the API of liferay. according to the docs I find this:

addEmailAddress(long userId, String className, long classPK, String address, int typeId, boolean primary) 

but I'm lost a little bit, I don't know how to use this method in the right way I have this.

EmailAddressLocalServiceUtil.addEmailAddress(user.getUserId(),className, classPK, emailAddress2, typeId, false)

I don't know how to get: -className -classPK -typeId

is there some way to obtain this parameters or I need to specify this parameters manually?

Some help?


Solution

  • EmailAddressService is used for storing emails related with different Liferay's entities (eg. User's Contact or Organization). In case of Contact (what you are propably trying to do) ClassName is Contact.class.getName() and classPK is user's contact object ID. TypeID is id of an emaill address type related with current entity (for Contact the types are Email Address 1, 2 and 3).

    I have a simple groovy script that does as follows:

    1. Get User from UserLocalServiceUtil
    2. Obtain proper className, classPK and typeID (default constant value from portal.properties)
    3. Add new email address to previously obtained user

    Code:

    import com.liferay.portal.model.Contact
    import com.liferay.portal.model.ListTypeConstants
    import com.liferay.portal.model.User
    import com.liferay.portal.service.EmailAddressLocalServiceUtil
    import com.liferay.portal.service.ServiceContext
    import com.liferay.portal.service.UserLocalServiceUtil
    import com.liferay.portal.util.PortalUtil
    
    User user = UserLocalServiceUtil.getUserByScreenName(PortalUtil.getDefaultCompanyId(), "someuser")
    
    String className = Contact.class.getName()
    long classPK = user.getContactId()
    
    int typeId = ListTypeConstants.CONTACT_EMAIL_ADDRESS_DEFAULT
    
    EmailAddressLocalServiceUtil.addEmailAddress(
            user.getUserId(), className, classPK, "[email protected]", typeId, false, new ServiceContext())