function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harshala P ShewaleHarshala P Shewale 

Apex Share is not working for Private Contact

There is a requirement to share Contacts. We have Contacts which are not directly linked to Account however linked through Junction Object.
Now If I want to share any Contact, then Contact is not visible to Users as they are Private (not linked to Account).

So I have changes OWD for Contact as "Private" and wrote Apex share code as below, It gets invoked through Custom Button on Contact Detail Page.
 
global class ContactShareController
{  
   webservice static string shareContact(string conId)
   {
       ContactShare con = new ContactShare();
       con.ContactId = conId;
       con.ContactAccessLevel = 'Edit';
       con.RowCause = 'Manual';
       con.UserOrGroupId = '00528000000kIX8';
       
       Database.SaveResult sr = Database.insert(con,false);
       string strMsg = '';
        
       if(!sr.isSuccess())
       {
            Database.Error err = sr.getErrors()[0];     
            strMsg = 'Unable to grant sharing access due to following exception: ' + err.getMessage();               
       }
       else
       {
            strMsg = 'Shared with ' + con.id;
       }    
 
       return strMsg;
   }
}

Code is working perfectly fine and I am able to see User gets added to "User and Group Sharing" section however when User logs in then he still gets "Insufficient Access" error for that contact.

Is there issue with Contact which not directly linked to Account? I am not sure where is it getting wrong.

Please let me know in case you have gone through such issue.
Pramodh KumarPramodh Kumar
Hi Harshala,

This links may help you.

https://help.salesforce.com/apex/HTViewSolution?id=000004005&language=en_US
http://salesforce.stackexchange.com/questions/44746/contact-visible-with-owd-private-even-though-user-doesnt-have-access


Thanks,
pRAMODH.
Harshala P ShewaleHarshala P Shewale
Hi Pramodh, 

Thanks for reply.
I had gone through this link, Its says,

To address this you can either set up an Account sharing rule to share the Accounts the Contacts are related to,
--> In my case Contacts are Private so this is not applicable.

or
you can change the Org Wide Default for Contacts to a value other than "Controlled by parent" ('Private' or 'Public Read-Only', for example) so that the Contact Sharing Rules will apply.

--> I have changed OWD for Contact to "Private" even though its not applying Contact Sharing rule.

Everything works perfectly when I link any Account to Contact, but thats not my business requirement.
Tushar RKTushar RK
Hi Harshala,

Please check following link,

Link: https://help.salesforce.com/apex/HTViewHelpDoc?id=contacts_sharing.htm&language=en

In section "Contact Sharing Considerations", it is mentioned that "You cannot manually share a private contact (a contact without an account).". So I think it will not be possible for you to share a contact without an Account (Account also needs to be shared with that user), Salesforce is itself restricting us from doing it.

Please let me know if this helps.

Regards,
Tushar Kumawat.
Harshala P ShewaleHarshala P Shewale
Hi Tushar,

Thanks for reply.

I have gone through lots of document as well as done a POC for apex share and came to conclusion thats

Apex Share does not work for standard object sharing as it works same as Manual sharing and manual sharing is not applicable for Private Contact.

So work around is as below -

1) Create Dummy Account per 10K Contacts to avoid Account Skew (https://developer.salesforce.com/blogs/engineering/2012/04/avoid-account-data-skew-for-peak-performance.html)Performance issue.
2) Create Rollup summary field on Account to check if it has less than or equal to 10K associated Contacts.
3) Create Validation Rule on Account which will fire on Rollup summary field count and will not allow more than 10K Contact associated with that Account.
4) Create another Dummy Account if previous Dummy Account reaches to 10K associated Contacts.

Let me know in case any one has better solution.
 
Tushar RKTushar RK
I think that is the best solution we can apply. As we cannot share any private contact unless it has an shared account, we must need to assign shared account record to the contact we want to share. I'll still check for other possible solutions and will let you know if I find any.