0
votes

I am testing a quick groovy script in the Liferay Control Panel Server Administration Script Console. I need to try and see if I can add a new user programmatically using the Liferay API.

I am new to groovy and maybe making a rookie mistake. I am not sure why I am getting the error and I've been trying and searching around for a solution.

I am using Liferay 6.2 EE sp7 on tomcat.

My script code is

import com.liferay.portal.model.Company;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.ContactConstants;
import com.liferay.portal.model.RoleConstants;

import com.liferay.portal.model.User;
import com.liferay.portal.service.CompanyLocalServiceUtil;
import com.liferay.portal.service.ContactLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.LocaleUtil;

long companyId = 52700 // my instance id
long creatorUserId = 52881 //my userid I am testing and running the script with

List<Long> userGroupIds = []

ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), actionRequest);

Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
long companyGroupId = company.getGroup().getGroupId();
List<Long> groupIds = []
groupIds.add(companyGroupId as long)

Role rolePu = RoleLocalServiceUtil.getRole(companyId, RoleConstants.POWER_USER);
List<Long> roleIds = []
roleIds.add(rolePu.getRoleId() as long)

try {

    User user = UserLocalServiceUtil.addUserWithWorkflow(creatorUserId, companyId, 
        false, "test", "test", false, "01234567",
        "01234567@abc.com", 0, " ", LocaleUtil.getDefault(),
        "First Add", " ", "Last Add", 0, 0, true,
        1, 1, 1970, "Job", groupIds,
        null, roleIds, userGroupIds, false,
        serviceContext);
} catch(e) {
    out.println("""<div class="portlet-msg-error">${e}</div>""")
    e.printStackTrace(out)
}

The error I am getting is:

groovy.lang.MissingMethodException: No signature of method: static com.liferay.portal.service.UserLocalServiceUtil.addUserWithWorkflow() is applicable for argument types: (java.lang.Long, java.lang.Long, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.util.Locale, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Boolean, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.String, java.util.ArrayList, java.util.ArrayList, java.util.ArrayList, java.util.ArrayList, java.lang.Boolean, com.liferay.portal.service.ServiceContext) values: [52881, 52700, false, test, test, false, 01234567, 01234567@abc.com, 0, , en_US, First Add, , Last Add, 0, 0, true, 1, 1, 1970, Job, [52741], [52741], [52709], [], false, com.liferay.portal.service.ServiceContext@5355c009] Possible solutions: addUserWithWorkflow(long, long, boolean, java.lang.String, java.lang.String, boolean, java.lang.String, java.lang.String, long, java.lang.String, java.util.Locale, java.lang.String, java.lang.String, java.lang.String, int, int, boolean, int, int, int, java.lang.String, [J, [J, [J, [J, boolean, com.liferay.portal.service.ServiceContext) at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1357) at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1343) at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:50) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at Script28.run(Script28.groovy:44) at com.liferay.portal.scripting.groovy.GroovyExecutor.eval(GroovyExecutor.java:58) at (lots omitted...)

I have tried to run with an equivalent beanshell script and also ran into issues that stem from the method addUserWithWorkflow.

So I tried using just the addUser method which worked without exceptions during script execution. However, it had issues with adding the appropriate groups and contacts and generated run time exceptions.

From the various liferay forum posts, I gather that the addUserWithWorkflow is a better approach. However, I run into invocation errors above.

UserLocalServiceUtil.addUserWithWorkflow API docs

Script tips I followed

I'd like to prove out that I can add a user properly using the API in the script console.

Please let me know if I need to provide more details.

Please help!

Update with the resolution based on Olaf's insight below:

I changed the script to define all the long arrays as "long[] userGroupIds = []" .... and I am past the error above. It really helped narrow it down and reading the Groovy documentation on an alternate way to define the arrays helped as well.

//long[] userGroupIds = {};
//long[] userGroupIds = [] as long[].
//List<Long> userGroupIds = []

long[] userGroupIds = []

... 
Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
long companyGroupId = company.getGroup().getGroupId();
//long groupIds[] = {companyGroupId};
//List<Long> groupIds = []
//groupIds.add(companyGroupId as long)
long[] groupIds = [companyGroupId];

Role rolePu = RoleLocalServiceUtil.getRole(companyId, RoleConstants.POWER_USER);
//List<Long> roleIds = []
//roleIds.add(rolePu.getRoleId() as long)
long[] roleIds = [rolePu.getRoleId()];
1

1 Answers

2
votes

Well, the error message says:

No signature of method: static ...UserLocalServiceUtil.addUserWithWorkflow() is applicable for argument types: [long list of argument types]

Compare the API documentation and its interface (one by one), and make sure you're using Liferay 6.2 (as you're linking the 6.2 API function.

A good guess is that this error message complains about four ArrayList parameters (close to the end of the list) while the API that you link would like to see long[]