I have been working with a customer who requires the ability to make a particular record private depending on a certain field. The first thing that came to mind was sharing the record using Apex. Jesse Lorenz put together a great article on how to work with Managed Sharing, and is a great place to start if you are considering using this feature of the platform.
Trigger ManageSharingOnMyObject on My_Object__c (after insert, after update) {
List<My_Object__Share> myObjShares = new List<M_Object__Share>();
for(My_Object__c myobj : trigger.new)
{
//if it is private only share with my manager
if(myobj.is_Private__c)
{
My_Object__Share mgrShare = new My_Object__Share();
//populate the share record with the ID of the record to be shared
mgrShare.ParentId = myobj.Id;
//now share with the users manager
mgrShare.UserOrGroupId = myobj.CreatedBy.ManagerId;
//options are edit,read, or all
mgrShare.AccessLevel = 'edit';
mgrShare.RowCause = Schema. My_Object__Share.RowCause.Manager_Access__c;
//add the share
myObjShares.add(mgrShare);
}
else //share with everyone
{
My_Object__Share allShare = new My_Object__Share();
//populate the share record with the ID of the record to be shared
allShare.ParentId = myobj.Id;
allShare.UserOrGroupId = 12345679;
}
}
if(! myObjShares.isEmpty())
{
// Insert all of the newly created Share records and capture save result
Database.SaveResult[] tripShareInsertResult = Database.insert(tripShares,false);
//exception handling etc, omitted for brevity
}
}