• Juan Camacho R
  • NEWBIE
  • 0 Points
  • Member since 2017
  • Salesforce Admin

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi,

 

I'm using following trigger for sharing records to hiring manager via apex

 

trigger Hiring_Manager_Job_Share on Position__c (after insert) {

    // We only execute the trigger after a Job record has been inserted 
    // because we need the Id of the Job record to already exist.
    if(trigger.isInsert){
     
    // Job_Share is the "Share" table that was created when the
    // Organization Wide Default sharing setting was set to "Private".
    // Allocate storage for a list of Position__Share records.
    List<Position__Share> jobShares  = new List<Position__Share>();

    // For each of the Job records being inserted, do the following:
    for(Position__c job : trigger.new){

        // Create a new Position__Share record to be inserted in to the Job_Share table.
        Position__Share hiringManagerShare = new Position__Share();
            
        // Populate the Position__Share record with the ID of the record to be shared.
        hiringManagerShare.ParentId = job.Id;
            
        // Then, set the ID of user or group being granted access. In this case,
        // we’re setting the Id of the Hiring Manager that was specified by 
        // the Recruiter in the Hiring_Manager__c lookup field on the Job record.  
        // (See Image 1 to review the Job object's schema.)
        hiringManagerShare.UserOrGroupId = job.Hiring_Manager__c;
            
        // Specify that the Hiring Manager should have edit access for 
        // this particular Job record.
        hiringManagerShare.AccessLevel = 'read';
            
        // Specify that the reason the Hiring Manager can edit the record is 
        // because he’s the Hiring Manager.
        // (Hiring_Manager_Access__c is the Apex Sharing Reason that we defined earlier.)
        hiringManagerShare.RowCause = Schema.Position__Share.RowCause.Hiring_Manager_Access__c;
            
        // Add the new Share record to the list of new Share records.
        jobShares.add(hiringManagerShare);
    }
        
    // Insert all of the newly created Share records and capture save result 
    insert jobShares;
        
    // Error handling code omitted for readability.
    }
}

 

and OWD for Position__c is Publice Read Only.

 

Now, when I create a new position record I get this error

Apex trigger Hiring_Manager_Job_Share caused an unexpected exception, contact your administrator: Hiring_Manager_Job_Share: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: AccessLevel (trivial share level Read, for organization with default level Read): [AccessLevel]: Trigger.Hiring_Manager_Job_Share: line 41, column 1

 

I can understand that this is trivial case and can be passed why my code is not working? What I am missing here?

  • February 24, 2012
  • Like
  • 0