I am trying to write a trigger that will share a record that is created on a custom object(Split__c) with all the groups the record creator is part of. The reason for this is to share the record created so that others in his group can edit it.

Below is the trigger that I created, but it is throwing some error on line 13 when looping through userToGroupMap map.

trigger PartnerCommissionSharing_new on Split__c (after insert, after update) {

    Map<Id, Id> userToGroupMap = new Map<Id, Id>();
    for(Split__c sc : Trigger.New) { 
        for (GroupMember gm: [SELECT GroupId, UserOrGroupId FROM GroupMember WHERE UserOrGroupId =:sc.CreatedById] ) {
            userToGroupMap.put(gm.GroupId, gm.UserOrGroupId);

    List<Split__Share> sharesToInsert = new List<Split__Share>();
    for (Split__c split: Trigger.new) {
        for (GroupMember gm: userToGroupMap.keyset()) {
            system.debug('GM Value :' + gm);
            sharesToInsert.add(new Split__Share(ParentId = split.Id, UserOrGroupId = gm, AccessLevel = 'Edit', RowCause = Schema.Split__Share.RowCause.Rep_Sharing__c));                         

    if(sharesToInsert.size() > 0){
        system.debug('Add user group list not empty');
        insert sharesToInsert;


The error that I receive is

Error: Compile Error: Invalid loop variable type expected Id was GroupMember at line 86 column 9

Is this approach the way to go sharing? Why do I get this error? How can I resolve it?


1 Answer 1


This error is very straightforward, but it points to a bigger issue. You have asked Salesforce to loop over every GroupMember in the set userToGroupMap.keyset(). But userToGroupMap has type Map<Id, Id>: it does not contain GroupMember records.

More importantly, though, you're not populating the Map in a way that does what you want to do. You appear to be intending to map from one User Id to a number of Group Ids. To do that, you would need a Map<Id, Set<Id>>.

Additionally, your code is populating the Map backwards:

    userToGroupMap.put(gm.GroupId, gm.UserOrGroupId);

Here, your key is the group Id, and your value is the User Id, which is the other way around from what you want.

So if you fixed those issues, you could make the compiler error go away by correctly pulling a Set<Id> out of the Map to iterate over:

for (Id gm: userToGroupMap.get(split.CreatedById)) {
  • Thank you David. I made some changes to code as below, but it still showing some error. Below is the change I made. <br/>Map<Id, Set<Id>> userToGroupMap = new Map<Id, Set<Id>>();<br/>Then used the below code to put the values as you said userToGroupMap.put(gm.UserOrGroupId, gm); <br/> The error it got is Error: Compile Error: Method does not exist or incorrect signature: void put(Id, GroupMember) from the type Map<Id,Set<Id>> at line
    – Gops
    Mar 14, 2018 at 22:55
  • 2
    @Gopi, it seems like you need a stronger grounding in how Apex and types work. GroupMember and Id are different types. I'd suggest you work through Trailhead and the Apex Developer Guide to get a better understanding.
    – David Reed
    Mar 14, 2018 at 23:30
  • @Gopi GroupMember is a different object type than User or Group. You share records with User or Group records, not with GroupMember records. It's not about how Apex works - you've just made a common mistake. Mar 14, 2018 at 23:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .