+ Start a Discussion
SkeeterSkeeter 

Apex sharing

I am trying to create apex sharing for read-only access on a private object to a custom lookup field, but it doesn't seem to be making the record visible.  

Any help is greatly appreciated.
 
Trigger Requester_Ticket_Sharing on BMCServiceDesk__Incident__c (after insert, after update) {
  
  if(trigger.isInsert){

  List<BMCServiceDesk__Incident__Share> ticketShares = new List<BMCServiceDesk__Incident__Share>();

  for(BMCServiceDesk__Incident__c inc : trigger.new){

  BMCServiceDesk__Incident__Share hiringManagerShare = new BMCServiceDesk__Incident__Share();

  hiringManagerShare.ParentId = inc.Id;

  hiringManagerShare.UserOrGroupId = inc.Requester__c;

  hiringManagerShare.AccessLevel = 'Read';

  hiringManagerShare.RowCause = Schema.BMCServiceDesk__Incident__Share.RowCause.Requester_Access__c;

  // Add the new Share record to the list of new Share records.
  ticketShares.add(hiringManagerShare);
  }
  // Insert sharing records and capture save result

  Database.SaveResult[] lsr = Database.insert(ticketShares,false);

  // Create counter
  Integer i=0;

  // Process the save results
  for(Database.SaveResult sr : lsr){
  if(!sr.isSuccess()){
  // Get the first save result error
  Database.Error err = sr.getErrors()[0];

  if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION

  && err.getMessage().contains('AccessLevel'))){
  // Throw an error when the error is not related to trivial access level.
  trigger.newMap.get(ticketShares[i].ParentId).
  addError(
  'Unable to grant sharing access due to following exception: '
  + err.getMessage());
  }
  }
  i++;
  }

  }

}

 
SandhyaSandhya (Salesforce Developers)