Legacy Knowledge Base
Published Jun. 30, 2025

RoleLocalServiceUtil.addRole creates the role in the main instance

Written By

Sorin Pop

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

Legacy Article

You are viewing an article from our legacy "FastTrack" publication program, made available for informational purposes. Articles in this program were published without a requirement for independent editing or verification and are provided"as is" without guarantee.

Before using any information from this article, independently verify its suitability for your situation and project.

Issue

  • We are trying to use the scripting console to create roles. Either we are not doing this correctly, or something is not working correctly.

    When creating the roles we use RoleLocalServiceUtil.addRole to which we pass a ServiceContext object on which companyId of the instance has been set. Roles, however, are being created on the main instance, and not the instance corresponding to the companyId set on serviceContext.

  • Steps to reproduce:

    1. Unzip Liferay-2023.Q3.2
    2. Startup
    3. Create a new instance with (for example) virtual host foo.localhost.com
    4. Check what companyId your new instance got - for example 12345
    5. Add foo.localhost.com to your hosts file
    6. In your main instance, go to control panel -> server administration -> script
    7. Execute the attached Groovy script (exchange the Config.companyId to the instance's company id)

    Actual result:
    Roles "Foo1" and "Foo2" are created on the main instance, but not in the instance with companyId 12345

    Expected result:
    Roles "Foo1" and "Foo2" are create on the instance with companyId 12345

Environment

  • 7.4
  • 2023.q3.2

Resolution

  • the culprit is the userId that you pass to the addRoles() method, and which eventually gets passed to the RoleLocalServiceUtil.addRole() API call.
     
    You obtain the userId like this:
     
    long currentUserId = Long.parseLong(userInfo.get("liferay.user.id"))
     
    and since you are running the script while you are connected to the main instance (with a user from that instance), this will return a userId belonging to the main instance, and you call the role creation method with such a userId.
  • So there is a certain inconsistency here: you want to create role in a certain instance, with a user ID from another instance, and probably this is what leads to this behaviour.
  • We suggest to somehow try to pass a proper userID, one belonging to a user from the instance where you want the role to be created. This has been verified to yield the expected results.

 

 

Did this article resolve your issue ?

Legacy Knowledge Base