How to query all user with salesforce license?

  • I want query the list of user with salesforce license

    List<user> user11=[SELECT name from user limit 5000];

    If this is not related to the visualforce tag, please edit your question to remove it. Else, some elaboration may be meaningful.

    Any visualforce user can also try this one that why i wrote this tag

    i need to filter this query with salesforce license

    But your question is not about visualforce, tags are meant to categorize questions.

  • Didn't find how to do it in a single query, but this gives all users with a Salesforce user licence type. Important to know is that you have to query this through the profile.

    Map<Id,Profile> profileIds = new Map<id,profile>([SELECT Id,UserLicenseId FROM Profile where UserLicenseId  in (SELECT Id FROM UserLicense where name ='Salesforce')]);
    List<user> standardProfileUsers = [select id from user where profileId in:profileIds.Keyset()];

    Update: Peter's answer is better: He shows that you can infact get these results in a single query, which is prefered for performance and cleaner in code.

    You can specify the relationship fields in the Where clause, e.g., `Select Id, Name, Profile.UserLicense.LicenseDefinitionKey From User Where Profile.UserLicense.LicenseDefinitionKey = 'SFDC'`

    How about you turn that into an answer so that I can give a true upvote ? :-)

    @Sdry - Sometimes I struggle with when to answer or comment. I really just looked at your code and thought of something versus thinking it out on my own, so I commented. :)

    It's a different, more elegant approach. In my opinion that's worth an answer of it's own.

    @Sdry - Alright. You convinced me. :-)

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used