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

1.8K    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

Interviews

Parent Categories