I have a use case where I need to update a logged in user profile based on some criteria. While doing so in apex I get the below error :

FIELD_INTEGRITY_EXCEPTION, Cannot change profile for current user: Profile ID

Is there any work around for this?


It's possible to achieve through admin credentials. I have executed and it's working fine for me.

//To get access token

String clientId = ConsumerKey__c;
String clientSecret = Client_Secret__c;
String username= Admin Username ;
String password= Admin password + SecurityToken get from your personal settings;

String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password; //Don't change anything 

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL+'/services/oauth2/token');//Note if my domain is set up use the proper domain name else use login.salesforce.com for prod or developer or test.salesforce.com for sandbox instance

HttpResponse res = h.send(req);

OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);

//Update your profile

Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
req1.setBody('{"userId":"0057F000003u35Y","profileId":"00e7F000002sKjK"}');//Send JSON body
req1.setEndpoint('https://login.salesforce.com/services/apexrest/ProfileUpdate?_HttpMethod=PATCH');//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
HttpResponse res1 = h1.send(req1);
system.debug('RESPONSE_BODY'+res1 .getbody());

JSON Deserailztion

/To get aouthentication detail Wrapper/

public class OAuth2{
 public String id{get;set;}
 public String issued_at{get;set;}
 public String instance_url{get;set;}
 public String signature{get;set;}
 public String access_token{get;set;}    

Restful apex class code

global with sharing class ProfileUpdate {

    global static String updateProfile(String  profileId, String userId) {

        update new User(Id=userId,ProfileId = profileId);

        return 'Profile Updated';

| improve this answer | |

As User Profile will be dependent on User License (profile field is dependent on User License Field) so the available profiles will be dependent on selected User License of that particular user. Actually being able to modify that through code would open up a whole new set of security risks.

To workaround this issue,

  1. Clone the user via apex.
  2. Take it in list
  3. Inactive the old user which has been cloned
  4. Insert new user, which are exist in list.

-------Sample code to workaround the issue-------

list<user> users = [SELECT Id,firstname, lastname, IsActive,ProfileId,UserType FROM User where profileId in :[select id from Profile where name = 'portal']]; // these users have different license, suppose "XYZ License" 
Profile p = [select id from profile where name='community']; // And these users have different license, suppose "ABC License" 
for(User u:users){ 
u.isactive = false; 
update users; 
list<user> newUserList = new list<user>(); 
for(User u1:users){ 
user NewUser = new user(); 
NewUser.allrequiredFields = u1.allrequiredFields; 
// put all required fields 
insert newUserList; 

'FIELD_INTEGRITY_EXCEPTION' error when profileID is updated via Apex

| improve this answer | |

A consultant wrote up a batch class that modifies user profile with a change in userLicense, and it is working and is not throwing an error

I was able to write a basic trigger with @future annotation, and that also worked. My trigger is on a platform event, and perhaps that is a factor, but it seems that this error is only thrown when the current/running user's profile ID is modified via apex to one with a different userLicense.

Batch, future, and queueable should all work.

| improve this answer | |

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.