3

Im creating a community user from an Apex code. When our internal users (non-admins) run the code they are getting 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);
10
  • Your class written "with Sharing" or "Without Sharing" ? Commented May 24, 2016 at 5:15
  • I have removed "With Sharing" too. public class GT_ActivationServices {} But still have the issue Commented May 24, 2016 at 5:19
  • it looks like field level permission issue. Check if system admin have field level permission set properly. Commented May 24, 2016 at 5:23
  • Thanks, it works well for system admins. But the issue is when it runs by other internal users. Commented May 24, 2016 at 5:28
  • 1
    Does you internal user profile has permission "Manage Internal Users"?
    – manjit5190
    Commented May 24, 2016 at 11:04

4 Answers 4

6

I was able to resolve the issue by adding 'without sharing' to all my framework classes which leads to create the portal user.

1
  • Solved my issues as well! Commented Jan 18, 2018 at 14:30
1

I faced the same type of issue. Without changing any code not able to create a user via APEX. In my case, I reduce API version. So API version lesser than 34 (<= 33) should fix this issue.

1
  • Fixed my issue. I still think there's something wrong with the code but this helps for now.
    – Semmel
    Commented Mar 14, 2018 at 10:05
0

You need to check your objects settings + field settings of that object in the profile(s) of the user. You need to see if it is writeable and that the field is also editable. If not, you need to enable it.

If there are more users with that profile and you don't want to give all of them access to the field, you can create a permission set and assign it to the users. You can also create a group and assign a permission set to it and then you can add the users you want to have access to the field in the group.

1
  • 1
    Thanks. But non Admin profiles has right to edit the ProfileId field on User object. Having said that it worked well previously and in FSB too Commented May 24, 2016 at 6:26
0

Administration wise, granting your integration user to Manage Users is too broad for a least-access security model. Updating your code to use without sharing is also not advisable.

However, you can set up the integration user as a Delegate Administrator, which will resolve the Field is not writeable: User.ProfileId error. This is also a stronger security model as you specify exactly which profiles and permission sets (and more) the integration user can set on any given community user. Note that the User Permissions Manage Customer Users, View Setup and Configuration, and View Roles and Role Hierarchy are required.

See the following documentation for more details. Set up a Delegated Administrator

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .