+ Start a Discussion

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';




















Change line#8 to:



cs.Id = c.Id;

You cannot do it because contactShare.contactId is not writable.To clarify this try following examples.

Not working(Not Editablr)


<apex:page standardController="contactShare">
    <apex:inputfield value="{!contactShare.contactId}"/>
    <apex:inputfield value="{!contactShare.ContactAccessLevel }"/>
    <apex:commandButton value="save" action="{!save}"/>

 but following working(Editable)


<apex:page standardController="opportunity">
    <apex:inputfield value="{!opportunity.AccountId}"/>
    <apex:commandButton value="save" action="{!save}"/>



ContactShare object contactId not writable???