Get Permission Set’s User email addresses

Have a need to get all the email addresses of users who are assigned a Permission Set?

It was something I ran into when needing to send emails from a Trigger.

Your use case is more than likley different, but here is a method to get a List<String> of email addresses by Permission Set.

To use the method, just pass in the API name of your PermSet:

Setup > Users > Permission Sets > [your permission set] > API Name in the top right. Ex: Expense_Approver_Final

// Get list of email addresses by Permission Set
public static List<String> getEmailByPermSet(String permsetname){
    List<Id> just_ids = new List<Id>();
    List<PermissionSetAssignment> users = [
        SELECT AssigneeId 
        FROM PermissionSetAssignment 
        WHERE PermissionSet.Name = :permsetname
    ];
    for(PermissionSetAssignment uid: users){
        just_ids.add(uid.AssigneeId);
    }

    List<String> just_email = new List<String>();
    List<User> email_list = [SELECT Email FROM User WHERE User.Id IN :just_ids];
    for(User usr: email_list){
        just_email.add(usr.Email);
    }

    return just_email;
}

Github Link