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
RajivRajiv 

Unable to set the contact id for contactShare

I am developing a trigger that will give access to certain contact records
that will meet some criteria. I need to update and insert a ContactShare
object for the same. However I am getting a compilation error

Error: Compile Error: Field is not writeable: ContactShare.ContactId at
line 8 column 9

trigger Share_Contact_For_Account on Contact (after insert, after update) {

    for (Contact c : Trigger.new) {


        ContactShare cs = new ContactShare();

        cs.ContactId = c.Id;
        cs.ContactAccessLevel = 'Edit';


    }
}


Any one got any ideas as to what I am doing wrong?

 

thanks,

-Rajiv

bob_buzzardbob_buzzard

Can you set it at construct time? e.g.

 

 

ContactShare cs = new ContactShare(ContactId=c.id);

 

 

RajivRajiv

The above code also generates the same error. The issue was also reported
in another thread.

http://boards.developerforce.com/t5/General-Development/ContactShare-object-contactId-not-writable/td-p/191637

Is there a solution to this?

thanks,

Rajiv

bob_buzzardbob_buzzard

This is related to your org wide sharing defaults, but the error message isn't that helpful.

 

I got the same in my dev org, but my sharing settings were that accounts are public read/write and contacts are controlled by parent.  This means that everyone already had full access to all contacts, and thus contact sharing rules aren't available.

 

I changed my contact org wide default to public read only, and was able to save the code without error.

RajivRajiv

Hi Bob, many thanks buddy. It's working fine now.

 

 

thanks,

Rajiv

Don Schueler 6Don Schueler 6
I have this fundamentally working....I had to switch Contact to Private from Controlled by Parent to both save the code AND to run the test class successfull. So far my code works since I do an Organization query to see if my Contact is Controlled by Parent, if it is I don't update the ContactShare object.  Note...as many might know, you can't even save your APEX with ContactShare cShare = new ContactShare(ContactId=cid, UserOrGroupId=UserInfo.getUserId(),ContactAccessLevel='Edit');   in it  if you have Controlled By Parent for Contact set as your default sharing. This is the only case I am aware of in SF where a configuration setting impacts the ability to save code or run a test. Seems wrong to me...we should be able to handle this in code and have it be OK.
Don Schueler 6Don Schueler 6
UPDATE...While all is OK on this as far as localized tests...the next problem is that you can't package this in a managed package!!!
Dependent class is invalid and needs recompilation: Class ApexBaseClass : Field is not writeable: ContactShare.ContactId
This is FRUSTRATING. Does anyone have a better approach for solving this in a Managed Package?