2

I'm working on a Portal Partner sharing trigger, and I need some help. On my Split__c there is field called Rep_1_Production_Site_Rep_v3__c which is a lookup on Account. And on this Account there is a field called Commission_Correction_User__c. Whenever the record gets created it needs to get shared to this user. So what I thought to do was to Map the Account and the Commission_Correction_User__c so whenever the Rep_1_Production_Site_Rep_v3__c was a valid partner user on the Split it will kick this off. I having an issue right now where I am trying to compare GroupMember to my AccountMap which holds the Commission_Correction_User__c

I don't believe the code will be needed, just want to put it in context

6-5-14 Update - I updated with Mark Ponds advice but am still getting an error at if (gMember.get(AccountMap.Commission_Correction_User__c) != null), I have also tried contains.. The error is D Initial term of field expression must be a concrete SObject: MAPId,MAPId,Account.. I see this error everywhere on the forums so I will continue to research it..

So

Pseudo**
If the Commission_Correction_User__c is in the GroupMember List add it to Split__Share

Now for the code

 Map < id, User > PartnerUsermap = new Map < id, User > ([select id, UserType, UserRoleId from user where UserType = 'PowerPartner' and isActive = true and isportalenabled = true
]);
List < GroupMember > GroupMemberList = new List < GroupMember > ([select GroupId, UserOrGroupId from GroupMember]);

List < Split__Share > CsShareList = new list < Split__Share > ();
List < Split__Share > Rep1ShareList = new list < Split__Share > ();
// map to be used later, key is the UserOrGroupId value

Map < Id, GroupMember > groupMemberMap = new Map < Id, GroupMember > ();
for (GroupMember gMember: GroupMemberList) {
    groupMemberMap.put(gMember.UserOrGroupId, gMember);
    system.debug('Group Member  ' + gMember);
}


for (Account commissionCorrectionAccount: [Select id, Commission_Correction_User__c, Name from Account where isPartner = true and Commission_Correction_User__c != null]) {
    if (groupMemberMap.containsKey(commissionCorrectionAccount.Commission_Correction_User__c)) {
        // get the groupmember record from the map
        GroupMember gMember = groupMemberMap.get(commissionCorrectionAccount.Commission_Correction_User__c);
    }
}
Map < id, Map < id, Account >> AccountMap = new Map < id, Map < id, Account >> ();
for (Account objA: [Select id, Commission_Correction_User__c, Name from Account where isPartner = true and Commission_Correction_User__c != null]) {
    system.debug('objA ' + objA);
    Map < id, Account > UserMap = AccountMap.get(objA.Commission_Correction_User__c);
    if (UserMap == null) {
        UserMap = new Map < id, Account > ();
        AccountMap.put(objA.Id, UserMap);

    }
    UserMap.put(objA.id, objA);
}

        if (AccountMap.containsKey(cs.Rep_1_Production_Site_Rep_v3__c)) {
            if (gMember.get(AccountMap.Commission_Correction_User__c) != null) { 
                system.debug('Inside rep1 = rep1 production site');
                Split__Share rep1Share = new Split__Share(ParentId = cs.id, AccessLevel = 'Read');
                Rep1ShareList.add(rep1Share);
            }

        }
    }

So to generalize my question How do I compare GroupMember (which is a list) to my AccountMap (which is a map)? I have tried .contains, ==, and .get with no luck I get an error

Initial term of field expression must be a concrete SObject:MAP Id,MAP Id,Account 
2

You can use a loop to turn the GroupMemberList into a map, keyed on the UserOrGroupId field and then that map can be used in your comparison operation.

Your pseudocode:

If the Commission_Correction_User__c is in the GroupMember List add it to Split__Share

In my example does this:

If the Commission_Correction_User__c ID value is in the GroupMember Map keyset, add it to Split__Share

Where the keys of the map are the User and Group ID values that you're trying to compare against. Putting them in a Map allows you to use the Map.containsKey() method to see if that user is in your collection.

You can confirm that the user referenced in the "correction user" field is in the map which you built like this:

if (groupMemberMap.containsKey(theAccount.Commission_Correction_User__c)) {

More holistic code sample:

List<GroupMember> groupMemberList = new List<GroupMember>([SELECT GroupId, UserOrGroupId FROM GroupMember]);

// map to be used later, key is the UserOrGroupId value
Map<Id, GroupMember> groupMemberMap = new Map<Id, GroupMember>();

for (GroupMember gMember : groupMemberList) {
    groupMemberMap.put(gMember.UserOrGroupId, gMember);
}

// records to be inserted during DML operation
List<Split__Share> splitSharesToInsert = new List<Split__Share>();

// determine which commission correction users are in the map and create the share records for them
for (Account commissionCorrectionAccount : [SELECT Id
                                                , Commission_Correction_User__c
                                                , Name
                                            FROM Account
                                            WHERE isPartner = true
                                                AND Commission_Correction_User__c != null]) {

    // however you determine when to add the share record logic
    if (groupMemberMap.containsKey(commissionCorrectionAccount.Commission_Correction_User__c)) {

        // get the groupmember record from the map
        GroupMember gMember = groupMemberMap.get(commissionCorrectionAccount.Commission_Correction_User__c);

        // create the share record for this commission correction user account
        splitSharesToInsert.add(new Split__Share(ParentId = yourRecord.id
                                                , UserOrGroupId = gMember.GroupId
                                                , AccessLevel = 'Read'));
    }
}
4
  • Trying to follow your example, but I'm getting pretty confused. Changing it to a map should work, but there is more to the code I didn't post
    – EricSSH
    Jun 5 '14 at 22:49
  • This means I can get rid of my for loops correct?
    – EricSSH
    Jun 5 '14 at 23:11
  • 1
    @EricSSH I understand that your example was a partial code sample, primarily because I don't know what it is intended to be doing. You'll have to adapt this pattern to what you're coding, but you are correct - you probably won't need the loops or maybe you can cut it down to just one loop. I would guess it will be the loop which creates the cs variable in your example code.
    – Mark Pond
    Jun 5 '14 at 23:14
  • The other part of my code is sharing to the person that creates the record, then I need to check if the person on the Rep accounts are also portal users so they will need to see the record also.. But I will continue to work on it, thanks for the help
    – EricSSH
    Jun 5 '14 at 23:16

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.