About this question
I'm creating a community user from an Apex code. When our internal users (non-admins) run the code they are getting the following error. (This was working fine when we test the feature in sandbox)
SObjectException: Field is not writeable: User.ProfileId
The code is
//Grab Community User Profile
Profile p = [Select Id, Name from Profile where Name = :COMMUNITY_PROFILE LIMIT 1];
User PortalUser = new User();
PortalUser.UserName = Con.Email+'-POC';
PortalUser.ContactId = Con.Id;
PortalUser.ProfileId = p.id;
PortalUser.Email = Con.Email;
PortalUser.EmailEncodingKey = 'UTF-8';
PortalUser.FirstName = Con.FirstName;
PortalUser.LastName = Con.LastName;
PortalUser.TimeZoneSidKey = 'America/Los_Angeles';
PortalUser.LocaleSidKey = 'en_US';
PortalUser.LanguageLocaleKey = 'en_US';
if (Con.LastName.length() >= 8){
PortalUser.Alias = Con.LastName.substring(0, 7);
}else{
PortalUser.Alias = Con.LastName;
}
SObjToInsert.add(PortalUser);