+ Start a Discussion
OnDem DevOnDem Dev 

Can Apex Managed Sharing be used in a Class declared with "with sharing" keyword?



I am using Private sharing model on a Custom object.


As a Non-Admin user, when i try to share the record to the other users with 'Edit' or 'Read' Access level in a Class which has "with Sharing" keyword, the following exception is thrown.

Field is not writeable: <<Custom Object>>__Share.RowCause


When i remove the "with sharing" keyword from the class, then it works fine.


Can any one tell what is the issue and what is the best solution.


Thanks in advance.






Message Edited by OnDem Dev on 01-18-2010 11:23 PM
I believe the RowCause field is a read only field, you shouldn't have to update it at all.  By default, when you use apex to create / update a sharing record, the RowCause is set to "Manual Sharing".  Not sure why you observed different behavior when you removed the "with sharing" keyword.
OnDem DevOnDem Dev



Thanks for the answer.

But i am not at all updating RowCause field. But it still throws Field is not Writeable exception.


Only if i remove the With Sharing keyword from the Class, then it works fine and shares the record.


Do you have any suggestions.

All the example classes which i found for Apex sharing dont use "With Sharing" keyword. Or it will be a Trigger example.