• Barsha Biswas
  • NEWBIE
  • 5 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
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;
    }

  }
}






 
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;
    }

  }
}






 

I need to add users with ceratin profile to be automatically  added to a public group . Any hints plz

  • May 31, 2012
  • Like
  • 0