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

Apex sharing for Contact Object

Hi All


I want to share the contact using trigger, when i tried the below code i am getting an error "Variable does not exist: ContactAccessLevel at line 29 column 9"


trigger ContactMakePublicTrigger on Contact (after insert) {

ID groupId = [select id from Group where Type = 'Organization'].id;
if (Trigger.isInsert) {
List<ContactShare> sharesToCreate = new List<ContactShare>();
for (Contact contact : {
ContactShare cs = new ContactShare();
cs.ContactAccessLevel = 'Edit';
cs.ContactId = contact.Id;
cs.UserOrGroupId = groupId;
if (!sharesToCreate.isEmpty())

insert sharesToCreate;





Please help me 


Thanks in advance.

Prashant TiwariPrashant Tiwari



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

This field (ContactAccessLevel ) must be set to an access level that is higher than the organization’s default access level for contacts.

for example: if Org wide default is 'Read Only', then ContactAccessLevel should be 'Edit'.


and also if for example 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.



Hope this will help you..!!



mark it as a solution  if it solves your problem as it may help others and Hit Kudos (click on star icon on left) if you think it meets your expectation :)





Prashant Tiwari





Hi Prashant


Thanks for your reply.


The OWD for Account and contact is private. I am getting error for all the fields of contact share object i.e. ContactAccessLevel,ContactId,UserorGroupId



but it is not throwing an error for this below line.


ContactShare cs = new ContactShare().


Please help me!!!






In my developer edition the above code is working perfectly.. but in test sandbox it is throwing an error...


please help me on this error...


Thanks in Advance!!!

Prashant TiwariPrashant Tiwari

This will not throw any error for  ContactShare cs = new ContactShare(). Just veryfy once again the default OWD of contact in your sandbox and Let me know if it works, As it worked for me.




Hi prashant


Thanks for your reply !!!


The contactShare cs = new contactshare() is not throwing an error but when i am accessing the fields of contactshare it is throwing an error


The below 3 lines are throwing an error. Error " ContactAccessLevel : Variable does not exist"


cs.ContactAccessLevel = 'Edit';

cs.ContactId = cont.Id;

 cs.UserOrGroupId =  at.UserId;