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
Praveen JhaPraveen Jha 

Add/remove user to specific group based on role when checkbox is selected - Very Very Urgent

I have one custom field "Notify me on new release" on user object whose data type is checkbox. I have added user to public group  based on role e.g. "Partner community manager"  . There are 50 users belongs to  "partner community manager" and i want to remove any one user from the group when i deselect "notify me on new relese " checkbox field in user detail page. .Right now i can add or remove  user to public group "alert" through below code. 

trigger AddingtoAlertPublicgroup on User (after insert,after Update) {
   List<GroupMember> grpMemlist = new List<GroupMember>();
    Set<Id> userIdsToProcess = new Set<Id>();
    Set<Id> usersToBeRemoved = new Set<Id>();
    Id alertGroupId;
    for(User Usr : Trigger.New){
        if(trigger.isInsert){
            if(Usr.Notify_me_of_releases__c){
               userIdsToProcess.add(Usr.Id); 
            }
        }
        if(trigger.isUpdate){
            if(Usr.Notify_me_of_releases__c && !trigger.oldMap.get(Usr.Id).Notify_me_of_releases__c){
                userIdsToProcess.add(Usr.Id);
            }
            if(!Usr.Notify_me_of_releases__c && trigger.oldMap.get(Usr.Id).Notify_me_of_releases__c){
                usersToBeRemoved.add(Usr.Id);
            }
        }
    }
    
    if(!userIdsToProcess.isEmpty() || !usersToBeRemoved.isEmpty()){
       List<Group> alertGroup = [SELECT Id FROM Group Where DeveloperName='Alert' LIMIT 1];
       if(!alertGroup.isEmpty()){
          alertGroupId =  alertGroup[0].Id;
       }      
    }
    for(User Usr : Trigger.New) {
        if(Usr.Notify_me_of_releases__c && userIdsToProcess.contains(Usr.Id) && alertGroupId != null) {
            GroupMember gm = new GroupMember();
            gm.GroupId = alertGroupId;
            gm.UserOrGroupId = Usr.Id;
            grpMemlist.add(gm);         
        }
    }
    if(!usersToBeRemoved.isEmpty() && alertGroupId != null){
        List<GroupMember> grpMemToBeDeleted = [SELECT Id FROM GroupMember WHERE GroupId = :alertGroupId AND UserOrGroupId IN :usersToBeRemoved];
        if(!grpMemToBeDeleted.isEmpty()){
            delete grpMemToBeDeleted;
        }
    }
    
    if(!grpMemlist.isEmpty()) {
        insert grpMemlist;
    }

}
PratikPratik (Salesforce Developers) 
Hi Praveen,

The trigger seem to be ok, what issue you are facing currently?

Thanks,
Pratik
PratikPratik (Salesforce Developers) 
+1

This is the working code  to achive this:
The groupid, you can query on group name.

Trigger:

Variable on User:  IsGroupMember__c (checkbox)
 
Trigger usedaddgrp on User ( after insert, after update) {
list<GroupMember> gmlistTrue = new list<GroupMember> ();
set<id> idremove = new set<id>();
list<GroupMember> gmlistFalse = new list<GroupMember> ();
    for(user u: trigger.new) {
    
        if(u.IsGroupMember__c) {
        
        GroupMember gm = new GroupMember();
        gm.GroupId = '00G900000021p3t';
        gm.UserOrGroupId= u.id;
        gmlistTrue.add(gm);       
        }
        
        if(!u.IsGroupMember__c && trigger.oldmap.get(u.id).IsGroupMember__c) {
        idremove .add(u.id);
        }
        
    }
    insert gmlistTrue;
    
    If(idremove.size()>0)
    {
        gmlistFalse = [select id from GroupMember where UserOrGroupId  IN : idremove];
        
        delete gmlistFalse ;
    }

}

Thanks,
Pratik
Praveen JhaPraveen Jha
Hi Pratik. 
Add user automatically in group based on "Notify me on new release" checkbox selected i have already done. But problem  I am facing  is when i have added user to public group  based on role e.g. "Partner community manager"  . There are 50 users belongs to  "partner community manager" and i want to remove any one user from the group when i deselect "notify me on new relese " checkbox field in user detail page. But when i deselect "notify me on new relese " checkbox  still user exist in group. Hope you can understand my problem. 
What we noticed is that when we select the said checkbox "Notify me on new release" , the user is automatically added to Alert public group with User as Membership Type. Now if you add multiple users by, for example, "Portal Roles", then remove users by deselecting the checkbox, users are still kept as members of the group. This is because the reason of membership is different - "Portal Roles" while the checkbox only applies to "Users" Membership type. 
Am i right????
PratikPratik (Salesforce Developers) 
Hi Praveen,

I was able to reproduce the issues as per above mentioned details.
As you have added the users through the Role in Public group that is the reason it will override the checkbox uncheck and trigger.

If you add users to public group through Users and not roles then the trigger will execute properly. 

If you want to have this requirement then i will suggest you to export the 50 users for the "Partner community manager" role and add them as users in Public group then the trigger logic will apply properly.

Thanks,
Pratik
 
Praveen JhaPraveen Jha
Thanks for your quick reply, but user is already added as role based and i don't want to add user manually. i have to remove specific user from the group. My workflow is set on this way when any article whose type is general email notification goes to the public group "Alert" . What i can do if any user don't want to receive the notification. I have to remove that user from the group. 
One solution is either we replace that usrer to specic new group. But that is very pathetic . Everytime i have to do it manually. 
Hope you understand my problem. Salesforce don't have unsubscribe facility so that user can unsubscribe email notification directly to click unsubscribe link. 
Praveen JhaPraveen Jha
I have added email to unsubscribe link in email template. But that is other issue. But it should be good if i remove user automatically when checkbox unselect from user detaol page
Praveen JhaPraveen Jha
Pratik can you please tell me what the trigger.oldMap… code is for??
PratikPratik (Salesforce Developers) 
Hi Praveen,

Trigger.oldmap will have old value for the rcord so you can compare the prior value with current value.

Thanks,
Pratik