I have a requirement where i need to Assign the UserA a particular license L2 when he is using a particular feature and then assign this license L2 to other userB when UserA is not working on this feature and assign Licence L1 to UserA. I will invoke a VFPage on a button click which will display the list of those users(with their current permission set) whose permission set can be swapped. Manager will select the users on this VF page and on a button click 'Change to L2' OR 'Change to L1' should be able to change the Permission set. I can write the visualforce page but struggling with code to swap the permission set. Also if you have better idea in mind to do this please share.


This link https://developer.salesforce.com/forums/?id=906F00000008oNzIAI might satisfy your requirement

Answer copied from the above link:

User License actually depends on the Profile, the relation is User-->Profile-->License. If you change the Profile, the license will change too.

You can also use anonymous apex execution combined with soql, depending on your level of experience and skill you can change 1000 user's profiles in a matter of seconds. For me, this is way faster then I could do it with export/data update/ date insert with any tool.

Be sure to test the soql query you use to query your users in advance, you do not want to run this on all users. I do not know if an error is thrown if you'd overwrite your admin user to a non admin profile, but you'd be in a nasty situation if that ever happens.

SELECT Id,firstname, lastname, IsActive,ProfileId,UserType FROM User WHERE   SOMETHING 

For instance :

list<user> users =  [SELECT Id,firstname, lastname, IsActive,ProfileId,UserType FROM User where profileId in :[select id from Profile where name = 'Standard User']]; // FILTER THIS !! you to not want to run this on all users !

Profile p = [select id from profile where name='Standard Platform User'];

for(User u:users){

u.profileId= p.id;
update users;

Only do this if you know what you're doing.

  • Hi @joe, although this post dates from a few years back, you might want to include the essentials of the link in your answer as it might become obsolete. Otherwise, this is considered a link only answer. Thanks! – glls Jan 8 '18 at 18:18
  • @glls done as requested – joe Jan 16 '18 at 15:57

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.