Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

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

Asked by Claudine Tippins May 9, 2023 6.4K views 2 answers
Share

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);

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 7, 2025

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.


Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.