I have a trigger that needs to share a commission split if they are the Commission_Correction_User on the Rep_1_Production_Site_Rep_v3__c. This is needed because they are Partner users, and will have to login through a portal. The problem that I am having right now is that instead of sharing with just the 1 user on the Rep_1_Production_Site_Rep_v3__c(Account), it is sharing with all of the Groups that is in my groupMemberMap.

Rep_1_Production_Site_Rep_v3__c is an Account and on that account is Commission_Correction_User, which is a User and that user is associated with a Group. So if Rep_1_Production_Site_Rep_v3__c is a partner user, go in and grab that User and share it with them.


This line is the issue, groupMemberMap will always have grpA (My AccountKey.keySet().. I can't wrap my head around how else to do it

-- I have multiple maps in play here

accountMap - Multidimensional map keyed to Commission_Correction_User.

Rep1SetMap - Holds the Rep_1_Production_Site_Rep_v3__c from the split. I did this in order to see if the Rep_1_Production_Site_Rep_v3__c is actually a partner user.

groupMemberMap - Holds all of the UserOrGroupIds

Here is my entire code http://pastebin.com/TjTNTShn, It's large and I don't think it is needed. In the end does anyone see why my code is sharing with all of the groups, and not just the one on the Rep_1 Account?

Map <Id, Account > Rep1SetMap = new Map <Id, Account>([Select Id, Commission_Correction_User__c, 
    Name from Account where isPartner = true and Commission_Correction_User__c != null and ID in:Rep1Set]);               

for(Split__c s: Trigger.new){
        for(Id grpA : accountMap.keySet()){
                    GroupMember gMember = groupMemberMap.get(grpA);             
                    splitSharesToInsert.add(new Split__Share(ParentId = s.id , 
                        UserOrGroupId = gMember.GroupId, AccessLevel = 'Read'));

                        Database.Saveresult[] rsr = Database.insert(splitSharesToInsert, false);                 
                }catch (NullPointerException e){ 
                    System.debug('NullPointerException' + e);                
  • As I understand your question, you're attempting to create a custom sharing group (or modify one) using Apex Sharing for a specific opportunity using the intersection of existing sharing groups. Is that correct? The problem you're having is that when you add a Partner Portal User, the Partner User gets added to all of your existing Sharing Group instead of creating a Custom Sharing Group for the Opportunity? Is that a good summary?
    – crmprogdev
    Jun 12 '14 at 17:59
  • No, every group in my org gets added to the split Sharing. groupMemberMap holds all my groups. The only person that should be added is the person on the Rep_1_Production ... I'll edit my question a little bit
    – EricSSH
    Jun 12 '14 at 18:08
  • It looks like you are looping through every group in the database on line 32, which is likely where your problem resides.
    – greenstork
    Jun 12 '14 at 18:17
  • @greenstork That part is sharing with the user group that creates the split. That part is working, now I'm trying to share with the User on the Rep 1 Account because they will also be Partner users and won't have visibility of the split
    – EricSSH
    Jun 12 '14 at 18:20

I agree that part of your problem lies here:

   for(Id grpA : accountMap.keySet()){

So, you need to be looking at Opportunities that matching this query:

 Rep1SetMap = new Map < id, Account > ([Select id, Commission_Correction_User__c, Name from Account where isPartner = true and Commission_Correction_User__c != null and ID in:Rep1Set]);               

And then adding Commission_Correction_User__c to the Sharing Group that matches your criteria.

I suggest you create a keyset from the query above to help with this. I'm assuming that's not GrpA since I don't see it in the code you've posted. That's where you need to find your intersection of ID's to work with.

  • I'm doing this on Commissions Splits, it is a custom object that was created. That shouldn't be an issue correct?
    – EricSSH
    Jun 12 '14 at 19:10
  • No, it shouldn't. My reference to Opps was because I knew that was the basis. Whatever you're using to manage it is immaterial. The point is that you need to find an intersection and I didn't see a keyset for the new map to pull the users you need from that map to add when doing your splits.
    – crmprogdev
    Jun 12 '14 at 20:34
  • I've been staring at this code for awhile now, and I relized I don't even need accountMap. I'm getting everything I need from Rep1SetMap. I need to figure out how to manipulate GroupMemberMap and Rep1SetMap
    – EricSSH
    Jun 12 '14 at 20:43
  • Happens to all of us. Glad I could help point you towards solving your issue. :)
    – crmprogdev
    Jun 12 '14 at 20:47

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.