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;
sharesToCreate.add(cs);
}
if (!sharesToCreate.isEmpty())
insert sharesToCreate;
}
}