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

Add user to specific group when checkbox is selected - Very Very Urgent

I have one custom field "Notify me on new release" whose data type is checkbox. I want to add those user to specific group name "Alert" when user select "Notify me on new release" check. How can i achieve this?? any help will be appericiated .
Best Answer chosen by Praveen Jha 18
Bhanu MaheshBhanu Mahesh
Hi Praveen,

try the 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_on_new_release__c){
               userIdsToProcess.add(Usr.Id); 
            }
        }
        if(trigger.isUpdate){
            if(Usr.Notify_me_on_new_release__c && !trigger.oldMap.get(Usr.Id).Notify_me_on_new_release__c){
                userIdsToProcess.add(Usr.Id);
            }
            if(!Usr.Notify_me_on_new_release__c && trigger.oldMap.get(Usr.Id).Notify_me_on_new_release__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_on_new_release__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;
    }

}


Regards,
Bhanu Mahesh

All Answers

Vijay NagarathinamVijay Nagarathinam
Hi,

My understanding about your question is whenever a new user creates and Notify me checkbox is true,  that user automatically added to one public group right?
trigger addintoPublicgroup on User (after insert) 
{
   AddUser.AddToGroups(trigger.newMap.keySet());

}
 
public class AddUser{

@future
public static void AddToGroups(Set<Id> userIds)
{
 //Get the groups that the user should be added to
Group g=[select Id from Group Where Name='test'];
 
 List<GroupMember>listGroupMember =new List<GroupMember>();  
 // loop the users that have been created
 for (User user : Trigger.new)
{
if(user.notifyme__c == true)
{
      GroupMember gm= new GroupMember(); 
      gm.GroupId=g.id;
      gm.UserOrGroupId = user.id;
      listGroupMember.add(gm);   
}
 } 
 insert listGroupMember;
}
}

 
Bhanu MaheshBhanu Mahesh
Hi Praveen,

try the 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_on_new_release__c){
               userIdsToProcess.add(Usr.Id); 
            }
        }
        if(trigger.isUpdate){
            if(Usr.Notify_me_on_new_release__c && !trigger.oldMap.get(Usr.Id).Notify_me_on_new_release__c){
                userIdsToProcess.add(Usr.Id);
            }
            if(!Usr.Notify_me_on_new_release__c && trigger.oldMap.get(Usr.Id).Notify_me_on_new_release__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_on_new_release__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;
    }

}


Regards,
Bhanu Mahesh
This was selected as the best answer
Sri549Sri549
Hello Praveen,
trigger AddToPG on User (after insert, after update) {
    List<GroupMember> GMlist = new List<GroupMember>();
    for(User U : Trigger.New) {
        if(U.Notify me on new release) {
            GroupMember GM = new GroupMember();
            GM.GroupId = '00GU0000001Zjeq';//fetch the alert group id using soql query on groups
            GM.UserOrGroupId = U.Id;
            GMList.add(GM);         
        }
    }
  if(!GMList.isEmpty()) {
        System.debug('Group Member List is ' + GMList);
        insert GMList;
    }
}
Use this above code.If you have any more doubts.
Please Mark as a Best Answer if your with requirement.

Thanks
Srinivas
Praveen Jha 18Praveen Jha 18
Thanks everone ...just one thing more can we do it for contact ?? contact having same field "Notify me on new release". when selected it will automatically added to public group name "Alert"
Praveen Jha 18Praveen Jha 18
Hi Bhanu,
The only problem we are facing is when i already added user to the group as per role base, then we are unable to remove that user even deselect the checkbox "Notify me on new release" 
Praveen Jha 18Praveen Jha 18
Hi Bhanu Mahesh

Can you please tell me what the trigger.oldMap… code is for.