function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sunny9222Sunny9222 

How to Mass update the user license using batch apex

How to Mass update the user license and profiles using batch apex for all the existing users 
NagendraNagendra (Salesforce Developers) 
Hi Sunny,

May I request you to please confirm that is there any specific reason to do the above using batch apex?

Tthe 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,  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;
Hope this helps.

Regards,
Nagendra.