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];
u.ProfileId=p.Id;
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.