I have a number of Contacts in the system, and a child object, Company Profile, with a lookup to Contact. This object has a picklist, "Area".

The idea is that each Contact should be shared with as many "groups" of users as there are different Areas for each Contact. So if Contact X has 3 Company Profiles with Area 1, Area 2 and Area 3 it should be shared with users from these three areas.

I have been analyzing how best to do this, and have a potential solution, but it's a bit of a kludge. I would like to know if there is a better approach since I am working with a clean slate here. Note that Accounts aren't present in the system.

My potential approach:

  • Set Contact Org-Wide Sharing to Private
  • Add Users to different Public Groups, one for each Area in the picklist.
  • Create a trigger to copy the values to a new field Text Area(255) in the Contact Record. E.g. Contact X would have
    Area1;Area2;Area3 from their Company Profiles.
  • Create a Contact Sharing rule to check if this Text Area field CONTAINS Area1 and if so, share with the Area1 group. Repeat for all Areas.

Potential issues:

  • Although the number of Areas per Contact should be small, using a Text Area field (255) there could potentially be overflow. I can't use Long Text Area (32000) because they can't be used in Contact Sharing Rules.
  • This approach seems fairly difficult to maintain, and unintuitive for the user.

Just have a User trigger create ContactShares when a User is added to a Group.

public static void shareContacts(Map<Id, Set<Id>> groupToContacts)
    List<ContactShare> contactShares = new List<ContactShare>();
    for (Id groupId : groupToContacts.keySet())
        for (Id contactId : new List<Id> ( groupToContacts.get(groupId) ))
            ContactShare contactShare = new ContactShare();
            contactShare.UserOrGroupId = groupId;
            contactShare.ContactId = contactId;

Obviously, you will need some more logic to figure out when a User is added to a group, and also to make sure you are not creating multiple shares for the same Contact. It seems like this would be a good starting point for your most flexible solution though.

| improve this answer | |
  • I think you're right that code is the way forward here – George S. Sep 11 '13 at 21:46

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.