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
Barsha BiswasBarsha Biswas 

How to automatically assign user to a public group , depending upon the value in the country field . So if the value in the country field is US , the user must be assigned to US public group and so on...

I am very new to apex coding and struggling with below use case . I couldnt find anything similar in the forum hence posting this question here if anyone may help

I have three public groups - US , Australia and India . I want to assign a user to a group upon creation , depending on the value in a custom ​Field on user ,using apex trigger . So if the value in custom field is US group , then the user must get assigned to the US group , if the value in custom field is Australia group , then the user must get assigned to the Australia group  and so on ....

I can get the trigger to run for a single value 
 
Trigger code

trigger AddToGroup on User (after insert,after update) {
AddUser.AddtoGroup(Trigger.newmap.keyset());
}
Apex class
 
public class AddUser {

    public static void AddtoGroup(Set<id> userIds)
    {
         Group g = [SELECT Id, DeveloperName FROM Group where developername='Australia_User_Group'];
        list <user> users = [select id,name from user where id IN : userIds];
        list<groupmember> listgroupmember = new list<groupmember>();
        
  
          for (user u: users){
              
              groupmember gm = new groupmember();
             
                  gm.Id = g.id;
                  gm.UserOrGroupId=u.id;
           
     }
            listgroupmember.add(gm);
        }
         insert listgroupmember;

}
I also tried to get the list of group ids and name pair in map and use if else condition as below but this fails and i know why , since i cannot specify to assign the group id to groupmmember id, where the developer name is so and so ... Can someone help in this please 
 
public class AddUser {

    public static void AddtoGroup(Set<id> userIds)
    {
      //group g = [select id from group where developername='US_User_Group'];
        Map<ID, Group> g = new Map<ID, group>([SELECT Id, DeveloperName FROM Group]);
        list <user> users = [select id,name from user where id IN : userIds];
        list<groupmember> listgroupmember = new list<groupmember>();
      for (ID idKey : g.keyset()) {
          for (user u: users){
              groupmember gm = new groupmember();
              group grp = g.get(idKey);
              if(u.User_Group__c=='US User Group'){
                  gm.Id = grp.Id;
                  gm.UserOrGroupId=u.id;
            } else 
                
                if(u.User_Group__c=='Australia User Group'){
                gm.Id = grp.Id;
                gm.UserOrGroupId=u.id;
            } else 
                
                if(u.User_Group__c=='Denmark User Group'){
                gm.Id = grp.Id;
                gm.UserOrGroupId=u.id;
            } 
          	
            listgroupmember.add(gm);
        }
         insert listgroupmember;
    }

  }
}






 
Best Answer chosen by Barsha Biswas
Barsha BiswasBarsha Biswas
Thanks Lori but this gives me an error "Variable does not exist: Id"

I could get this to work using custom metadata as suggested by @Roger Wicki in this thread: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000913K

1. Create a Custome Metadata
2. Add all assignments for your criteria (like user group => Group.DeveloperName)
3. Write a code that hands the criteria (e.g. user group) to the mapping of the Custom Metadata and assign the group that way

All Answers

Lori LiuLori Liu
Hi Barsha,

I think you should update the following code:

gm.Id = g.id;

to

gm.GroupId = g.id;
Barsha BiswasBarsha Biswas
Thanks Lori but this gives me an error "Variable does not exist: Id"

I could get this to work using custom metadata as suggested by @Roger Wicki in this thread: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000913K

1. Create a Custome Metadata
2. Add all assignments for your criteria (like user group => Group.DeveloperName)
3. Write a code that hands the criteria (e.g. user group) to the mapping of the Custom Metadata and assign the group that way
This was selected as the best answer