0

I have been trying to check the current user is exists in public groups or not using trigger on Contact object before insert. I was wondering whether this is right way to check it or not? Exactly what I want to achieve is to check and prevent current user from creating new contact which is not in particular groups. Any kind of suggestions are highly appreciated. Here is my trigger:

  trigger ValidateUserToImport on Contact (before insert) {



public String Currentusergroup {get;set;}

   List<groupmember> lst = [select group.name,group.DeveloperName from groupmember where UserOrGroupid=:UserInfo.getUserId()];
   //List<Contact> contacts = [select Home_Company__c, Working_Host_Company__c from Contact];

  for( Contact c : trigger.new )
  {
     for(groupmember lst2: lst)
      {
        if(c.Working_Host_Company__c != lst[0].group.DeveloperName)
        {
            c.addError('Insufficient rights');
        }
        else
      {
        insert c;
      }
      }

  }

}

I tried like this, still wondering whether this is right way to do as follows:

trigger ValidateUserToImport on Contact (before insert) {

  public String Currentusergroup {get;set;}

   List<groupmember> lst = [select group.name,group.DeveloperName from groupmember where UserOrGroupid=:UserInfo.getUserId()];
   List<Contact> work_cmpny = [select Home_Company__c, Working_Host_Company__c from Contact];

  for( Contact c : trigger.new )
  {
     for(groupmember lst2: lst)
      {
        if(!c.Working_Host_Company__c.Contains(lst2.group.DeveloperName))
        c.addError('Insufficient rights');
      }
  }

}
1

You could do something like the following (untested) code. It is building a Set of all the groups the current user is in. It will then check each Working_Host_Company__c on the Contact, if the user is not in a group for that Contact then an error is added.

   Set<String> groupNames = new Set<String>();
   for (GroupMember gm : [select 
                             group.name,
                             group.DeveloperName 
                          from GroupMember 
                          where UserOrGroupId = :UserInfo.getUserId()]) {
       groupNames.add(gm.group.DeveloperName);
   }

   for(Contact c : trigger.new) {
      if (!groupNames.contains(c.Working_Host_Company__c)) {
           c.addError('Insufficient rights');
      }
   }
1
  • I highly appreciate for this snippet and recommendations. It got solved. Sorry for getting back late.
    – OnkarDhane
    Apr 19 '15 at 18:16
-1
  1. Collect the developer(api) names of groups in a set.

  2. Iterate over the inserted(on before insert) Contact records and collect the Ownerids in a set.

3.

for(GroupMember objGM: [select Id,  UserOrGroupId FROM GroupMember where UserOrGroupId IN : set collected in step 2 AND Group.DeveloperName In : set in step 1])
{
   setUserId.add(objGM.UserOrGroupId);
}
  1. Again Iterate over the inserted(on before insert) Contact records and do the following:

    if(!setUserId.contains(contact.ownerId)) contact.addError('Insufficient access');

1
  • 1
    Theres a few things wrong with your solution. It is supposed to be checking the current users groups not the owner. If multiple Contacts are added and the user is in a group for 1 Contact but not the rest your code will not throw an error.
    – BarCotter
    Apr 14 '15 at 14:13

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.