I'm trying to add Partner Users to a Public Group based on a checkbox. It works fine for Users but I can't seem to find how to correctly reference the Partner User in my trigger. As sample is below:

trigger AddToPG on Contact (after insert, after update) {
    List<GroupMember> GMlist = new List<GroupMember>();
    Set<String> contactEmails = new Set<String>();

    for(Contact con : Trigger.New) {
        //create a set with the contact email addresses

    //query for the related users and put them in a map,
    //where the key is the email and the value is the user
    Map<String, User> emailUserMap = new Map<String, User> ();

    for(User aUser : [select id, email from User where email in : contactEmails]){
        emailUserMap.put(aUser.email, aUser);


    List<Id> userIdList = new List<Id>();

    for(Contact con : Trigger.New) {
        if(con.Flow_Concrete__c == TRUE) {    

    //dymanically get the get group id.
    Group theGroup = [select id from Group where Name = 'Concrete'];

    if(null != theGroup){
        //call the contact trigger helper if the group exists. 
        //This method adds the user to the group
        ContactTriggerHelper.addUsersToGroup(theGroup.id,userIdList );
  • 1
    The user object contains a reference to the contact for partner users. Is there a reason you're not using that field instead of the email? – gNerb Mar 16 '18 at 14:46

I'm not sure if this will actually fix your issue, but I modified your code to be a bit cleaner:

trigger AddToPG on Contact (after insert, after update) {
    // List<GroupMember> GMlist = new List<GroupMember>(); // You're code doesn't seem to actually use this variable?
    Set<Id> usersToAdd = new Set<Id>();
    Id partnerProfileId = [
        SELECT Id
        FROM Profile
        WHERE Name = 'Partner Profile Name'

    // Find your partner users based on the contactId instead of email
    // More reliable
    List<User> partnerUsers = new List<User>([
        SELECT Id, ContactId
        FROM User
        WHERE ContactId IN :Trigger.New
            AND ProfileId = partnerProfileId

    // Loop through users you find, not the contacts in the trigger
    // This loop will run at most once per contact, but often far fewer times
    // Where as looping through contacts guarantees the worst case scenario, every time
    for (User u : partnerUsers) {
        if (Trigger.newMap.get(u.contactId).Flow_Concrete__c == TRUE) {    

    // I would consider finding a way to get rid of this query
    // Hardcoding the Id is far from ideal but in sfdc limits are king
    // Alternatively you can use custom meta-data or custom settings
    Group theGroup = [
        SELECT Id 
        FROM Group 
        WHERE Name = 'Concrete'

    if (theGroup != null) {
        ContactTriggerHelper.addUsersToGroup(theGroup.id, usersToAdd);
| improve this answer | |
  • Thanks for the help! I should mention that I have 6 different check boxes that the code has to loop through as each contact/user can have a different combo of selections and there's a helper class that uses the List<GroupMember>. I'll see if I can modify it correctly to cover the remaining checkboxes. – Angela H Mar 16 '18 at 16:00

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.