How to resolve the error: field is not writeable: user.profileid?

3.2K    Asked by ClaudineTippins in Salesforce , Asked on May 9, 2023

 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);
Answered by David Edmunds

To resolve the error: field is not writeable: user.profileid


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.



Your Answer

Answer (1)

The error "Field is not writeable: User.ProfileId" in Salesforce typically occurs when attempting to update the ProfileId field of a User record. This field is read-only for standard users and can only be modified under specific conditions. Here’s how you can resolve it:

1. Check Your User Permissions

Ensure that the user making the update has the Modify All Data or Manage Users permission.

Only users with System Administrator privileges or a profile with elevated permissions can change the ProfileId.

2. Use the Correct Context (System Mode vs. User Mode)

If you're running the code in user mode, certain fields become read-only.

Try running the code in system mode using without sharing in Apex if needed.

3. Update Through Apex Code (Only for System Admins)

If you have the necessary permissions, you can update the ProfileId using Apex:

User u = [SELECT Id, ProfileId FROM User WHERE Id = :userId];
u.ProfileId = 'NewProfileId';
update u;

Ensure that the new ProfileId exists and is valid.

4. Use the Salesforce UI for Profile Changes

If Apex or API methods don’t work, manually update the ProfileId via Setup > Users > Edit User.

5. Check for Triggers or Flows

Ensure there are no triggers, validation rules, or flows preventing the ProfileId update.

If none of these solutions work, check your Salesforce edition and API version to see if there are restrictions on Profile updates.


1 Week

Interviews

Parent Categories