Message Boards

how to create role via programming code

Scarletake Bwi, modified 3 Years ago.

how to create role via programming code

Expert Posts: 326 Join Date: 12/20/10 Recent Posts

hi

i just use liferay 7.3.5

i want to create many roles for init.

but i got problem

in RoleServiceUtil, i got 

ClassName cm=ClassNameLocalServiceUtil.getClassName(Role.class.getName());
        RoleServiceUtil.addRole(cm.getClassName(), classPK, name, titleMap, descriptionMap, type, subtype, serviceContext)

but about classPK, i know it's roleid of role_, but  i cannot find createRole in RoleServiceUtil, i cannot get roleid before it be created.

or i have to give null and update from table manually?

 

i also try

ClassName cm = ClassNameLocalServiceUtil.getClassName(Role.class.getName());
 Role role = RoleServiceUtil.addRole(cm.getClassName(), 0, name, null, null, RoleConstants.TYPE_REGULAR, null, null);
role.setClassPK(role.getRoleId());
RoleServiceUtil.updateRole(role);

but, RoleServiceUtil.updateRole doesn't support i give Role. and from api, i didn't see any mether for update classPK.

very thank you.

Scarletake Bwi, modified 3 Years ago.

RE: how to create role via programming code

Expert Posts: 326 Join Date: 12/20/10 Recent Posts

i try and got the answer

RoleServiceUtil.addRole(cm.getClassName(), 0, name, null, null, RoleConstants.TYPE_REGULAR, null, null);

when you put 0 as classPK, liferay will use the PKey of target table.