I have a method that changes a user's profile. It works well when the old and the new profiles belong to the same user licence, but when they're from different user licences, it throws an error.

Here's my code:

public static void changeProfile(ID userId, ID profileId){
    User u=[select id, name, contactid, profileId from User where id=:userId];
    Profile p=[select id, name from profile where id=:profileId];
    update u;

And this is the error:

System.DmlException: Update failed. First exception on row 0 with id 0051I000000aVQbQAM; first error: FIELD_INTEGRITY_EXCEPTION, You can only assign profiles that are compatible with the user's license. For a list of compatible licenses and profiles, see the Salesforce Help.: [ProfileId]

How can I solve this?

I'm working with communities, by the way, and the profiles that I'm working with belong to Customer Portal Manager Licence and Customer Community Licence.

  • Ok, but that page is suggesting to create a new user every time I want to change the profile. Right now I'm using developer edition, so there should be no problem in creating as many users as I want, but I don't think that solution is very efficient, since the org will soon have a lot of inactive users. Is there no other way? – Daniel.O Nov 1 '17 at 22:49

You can't change the user's license type after creation, therefore you cannot change their profile to a new license type than what they had at creation. This restriction is probably in place because changing the user's license type might cause orphan data or break other database constraints. For now, you need to create new users. And yes, you will have lots of inactive users, but they don't count against your license limits. Consider setting their usernames to nonsense values when you deactivate them so they don't conflict with real email addresses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.