1

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?

1

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.setBody(reqbody);
req.setMethod('POST');
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.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setBody('{"userId":"0057F000003u35Y","profileId":"00e7F000002sKjK"}');//Send JSON body
req1.setMethod('POST');
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

@RestResource(urlMapping='/ProfileUpdate/*')
global with sharing class ProfileUpdate {

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

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

        return 'Profile Updated';
      }

}
0

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 
newUserList.add(NewUser); 
} 
insert newUserList; 

'FIELD_INTEGRITY_EXCEPTION' error when profileID is updated via Apex

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.