Customer Community Plus User Cannot Add Other User
Explanation
Take a look at sObjects That Cannot Be Used Together in DML Operations. It is not your call to UserInfo.getUserRoleId()
that is causing the error. You get the error because you are setting the UserRoleId
field to a non-null value.
Based on the below, you could in theory write a class that can insert a User
with a UserRoleId
if you set the API Version
to 14.0
or below. I wouldn't recommend it, however, as there are certainly better workarounds.
- Don't specify
UserRoleId
or specify it asnull
- Setting the
UserRoleId
in atrigger
or@future
method might work.
Example
Any code sample along these lines will pass:
insert new User(
// required fields
UserRoleId = null
);
insert anotherRecord;
API Version 15.0 +
Insert
You can insert a user in a transaction with other sObjects in Apex code saved using Salesforce API version 15.0 and later if UserRoleId is specified as null.
Update
You can update a user in a transaction with other sObjects in Apex code saved using Salesforce API version 15.0 and later if the following fields are not also updated:
- UserRoleId
- IsActive
- ForecastEnabled
- IsPortalEnabled
- Username
- ProfileId
Prior Versions
Insert
You can insert a user in a transaction with other sObjects in Apex code saved using Salesforce API version 14.0 and earlier.
Update
You can update a user in a transaction with other sObjects in Apex code saved using Salesforce API version 14.0 and earlier.