-3

whenever a user is created I am adding that user to chatter group based on region and also based on region field if user region is changed from Asia to Europe then I am trying to remove user from Asia group and adding to Europe chatter group.

Can anyone please help me with this ?

Class :

@future
public static void groups(Set<Id> userIds)
{
List<User> users=[select id,UserTheater__c,Name,Username from User where id in :userIds];
List<String> groups=new List<String>{'Japan','London','Australia'};

List<CollaborationGroup> chatGps=[select id, Name from CollaborationGroup where name in :groups];
List<CollaborationGroupMember> deletemember = new List<CollaborationGroupMember>();

List<CollaborationGroupMember> chatGpMem=new List<CollaborationGroupMember>();
for (User user : users)
{
    for (CollaborationGroup chatterGroup : chatGps)
    {
        CollaborationGroupMember c =  new CollaborationGroupMember();
        if(user.reg__c == 'Japan' && chatterGroup.name == 'Japan' ){   
            c.CollaborationGroupId=chatterGroup.id;
            c.MemberId = user.Id;
            deletemember = [select Id, MemberId from CollaborationGroupMember where (CollaborationGroup.Name =: 'London' OR CollaborationGroup.Name =: 'Australia') AND MemberId =: c.MemberId limit 1 ];
            chatGpMem.add(c);
        }
        else if(user.reg__c == 'London' && chatterGroup.name == 'London' ){
            c.CollaborationGroupId=chatterGroup.id;
            c.MemberId = user.Id;
            deletemember = [select Id, MemberId from CollaborationGroupMember where (CollaborationGroup.Name =: 'Japan' OR CollaborationGroup.Name =: 'Australia') AND MemberId =: c.MemberId limit 1 ];
            chatGpMem.add(c);
        }
        else if(user.reg__c == 'Australia' && chatterGroup.name == 'Australia' ){
            c.CollaborationGroupId=chatterGroup.id;
            c.MemberId = user.Id;
            deletemember = [select Id, MemberId from CollaborationGroupMember where (CollaborationGroup.Name =: 'London' OR CollaborationGroup.Name =: 'Japan') AND MemberId =: c.MemberId limit 1 ];
            chatGpMem.add(c);
        }
}
if(chatGpMem.size()>0) insert chatGpMem;
if(deletemember.size()>0){
delete deletemember;
}
}

Trigger:

trigger add on User (after insert,after Update) 
{
   addUser.AddToGroups(trigger.newMap.keySet());
}
  • 2
    Does this code even work? You've clearly not included all the code that is relevant to your issue. Please edit your question with more detail, including what you've tried and why it didn't work. – Nick Cook Aug 30 '16 at 15:23
  • @NickCook My code is working properly. The issue is I want to avoid Soql inside for loop and also as it is firing evrytime it is edited in debugs it is showing that error and also I included everything what I used in my actual class – Anusha Aug 30 '16 at 15:46
  • You seem to already know how to do it. Just use a similar pattern to what you used for the chatGpMem list. – Sebastian Kessel Aug 30 '16 at 16:04
1

1.In order to tackle the evoking on every update event issue, I would suggest to use, trigger.oldMap values so that you check if the oldValue is different to the new value and it corresponds to your logic. This would look something like:

Map<ID,User> oldUsers =  Trigger.oldMap;
Set<ID> ids = new Set<ID>();

for( User u : Triger.new){
   if(oldUser.get(u.id).Reg__c=='Europe' and u.reg__c=='Asia')
     ids.add(u.id);
 }

 List<User> users=[select id,UserTheater__c,Name,Username from User where id in :userIds and id in:ids];

2.In order to tackle the SOQL statements inside the loop I suggest to create a map outside the loop that maps the CollaborationGroupMember object to MemberId.

List<ColleborationGroup> cgs1 = [select Id, MemberId from CollaborationGroupMember where (CollaborationGroup.Name =: 'London' OR CollaborationGroup.Name =: 'Japan') ];
Set<Id> deleteIds  = new Set<Id>();
Map<id, id> cgmMap1 = new <id, id>();

for(ColleborationGroup cg : cgs1)
  cgmMap1.put( cg.memberid ,cg.id);

Then inside your condition you would have something like: Id cgDeleteID = ccgMap1.get(chatterGroup.id);

Then you add the id to the deleteIds set;

 if(cgDeleteID!=null)
  deleteIds.add(cgDeleteID)

Then outside the loop you can select all the CollaborationGroupMember based on the deleteIds and delete them.

Note: this example is for the chatterGroup.name == 'Australia' condition. You would need to create a different map for London and Japan and do as I showed you above.

| improve this answer | |

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.