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
Isaac GomolkaIsaac Gomolka 

Assign Feature Licenses in apex

Hi everyone. Im working on a VF/apex page that will help our admins clone most of a users info, input a little info, and then create a new user from it. Some of the things that need to be cloned over are the feature licenses and permission set licenses. Im wondering how I actually work with the licenses in apex. I thought they would be held in User fields, but its not. I found the list of licenses and stuff under Company Info in SF, but they dont have the IDs listed or anything. Are there even IDs for these?

I basically want to be able to do something like this:
newUser.MarketingLicense = clonedUser.MarketingLicense

I'll basically do that for every license and so then if the user we are trying to clone has any licenses, it will assign those licenses to the new User too. Any help on how to do this would be so greatly appreciated. Thank you so much!
Raj VakatiRaj Vakati
You need to use UserLicense object.. 
The UserLicense object is currently used by bulk user creation to determine the user license to which each profile and permission set belongs. For example, if you use the API to create portal users and you want to know which profile belongs to each portal user license, you can query this object for each profile and check the LicenseDefinitionKey to identify the associated user license.

​https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_userlicense.htm