Apex Error: Field is not writeable

  • I am new to writing Apex Triggers. I wrote this trigger by referring few examples, but now i end-up with ContactAccessLevel field is not writable Error.

    Use Case: In contact object, I have a field called xrx_mf_crm__Region_1__c. If the value of this field matches the public groups name, then the record should be shared with that public group with read/write access.

    trigger ContactShareByRegion1 on Contact (After Insert, After Update) 
        Group grp = new Group();
        for(Contact ContactObj : [select Id, xrx_mf_crm__Region_1__c from  Contact where Id IN: Trigger.New]){
            string Region = ContactObj.xrx_mf_crm__Region_1__c;
            List<ContactShare> sharesToCreate = new List<ContactShare>();
            if (Region) {
                ID groupId = [select id from Group where Name = :Region].id;
                ContactShare cs = new ContactShare();
                cs.ContactAccessLevel = 'Edit';
                cs.ContactId = contactObj.Id;
                cs.UserOrGroupId =  groupId.id;
            if (!sharesToCreate.isEmpty())
                insert sharesToCreate;

    Set contact as private or read the only in sharing setting. ?

  • Jenny B

    Jenny B Correct answer

    6 years ago

    In Setup -> Security Controls -> Sharing Settings, you would need contacts to be set to Private vs Controlled by Parent in order to be able to write to that field.

    On a side note, you may want to also look into moving your groupId query outside of your for loop. You should also be able to access the xrx_mf_crm__Region_1__c field without querying for it by looping through the list of records in the trigger something like:

    for (Contact contact : Trigger.new)

    It looks like there is a blog post that should help you through this trigger too, here.

    Thanks Jenny for Answering my query. Changing the sharing setting to private helped to resolve the issue.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used