+ Start a Discussion

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) {

  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.
  // 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){
  // 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.
  'Unable to grant sharing access due to following exception: '
  + err.getMessage());



SandhyaSandhya (Salesforce Developers)